1、模型
//Province model
public function cities(){
return $this->hasMany(City::class);
}
//City model
public function provinces(){
return $this->belongsTo(Province::class);
}
2、控制器
public function index()
{
$provinces=Province::with('cities')->get();
return view('index',compact('provinces','cities'));
}
3、视图
@foreach ($provinces as $province)
{$province->provinceName}
@foreach ($provinces->cities as $city)
{$city->cityName}
@endforeach
好像有些问题,但不知道是哪里。