我有三张桌子
和我建立了这种关系
serviceCat
public function serviceSubCat()
{
return $this->hasMany('ServiceSubCat');
}
serviceSubCat
public function ServicesCat()
{
return $this->belongsTo('ServiceCat', 'cat_id');
}
public function ssContent()
{
return $this->hasMany('SubCatContent');
}
subCatContent
public function ssCat()
{
return $this->belongsTo('ServiceSubCat', 'sub_cat_id');
}
我需要从三个表中获取葡萄数据才能使用它们。
这是我的控制器
public function show($id)
{
$serCat = ServiceCat::where('id', $id)->first();
$getId = $serCat->sub_status;
if ($getId == 1) {
$subCats = ServiceSubCat::with('ssContent')->where('cat_id', $id)->get();
//dd($subCats);
return View::make('portal.services.servicesDetailsList', compact('serCat', 'subCats'));
} else {
return View::make('portal.services.servicesDetails');
}
}
现在我收到错误
未找到专栏
我需要获取cat_name,sub_cat_name,title,content
答案 0 :(得分:0)
确保您的关系定义为:
<强> serviceCat 强>
public function serviceSubCat()
{
return $this->hasMany('ServiceSubCat', 'cat_id');
}
<强> serviceSubCat 强>
public function ServicesCat()
{
return $this->belongsTo('ServiceCat', 'cat_id');
}
public function ssContent()
{
return $this->hasMany('SubCatContent', 'sub_cat_id');
}
<强> subCatContent 强>
public function ssCat()
{
return $this->belongsTo('ServiceSubCat', 'sub_cat_id');
}
您的控制器
public function show($id)
{
$serCat = ServiceCat::where('id', $id)->with('serviceSubCat.ssContent')->first();
$getId = $serCat->sub_status;
if ($getId == 1) {
return View::make('portal.services.servicesDetailsList', compact('serCat'));
} else {
return View::make('portal.services.servicesDetails');
}
}
然后在您的视图中,您可以这样做:
{{ $serCat->cat_name }}
@foreach ($serCat->serviceSubCat as $ser)
{{ $ser->sub_cat_name }}
@foreach ($ser->ssContent as $subCatContent)
{{ $subCatContent->title }}
{{ $subCatContent->content }}
@endforeach
@endforeach