php - Laravel : Select from three tables using eloquent -
i have 3 tables
- service_cats (id - cat_name)
- service_sub_cats (id - cat_id - sub_cat_name)
- sub_cat_contents (id - sub_cat_id - title - content)
and made relations
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'); }
i need grape data 3 tables use them.
here controller
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'); } }
now error
column not found
i need cat_name, sub_cat_name, title, content
make sure relations defined as:
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'); }
your controller
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'); } }
then in view can as:
{{ $sercat->cat_name }} @foreach ($sercat->servicesubcat $ser) {{ $ser->sub_cat_name }} @foreach ($ser->sscontent $subcatcontent) {{ $subcatcontent->title }} {{ $subcatcontent->content }} @endforeach @endforeach
Comments
Post a Comment