php - Laravel : Select from three tables using eloquent -


i have 3 tables

  1. service_cats (id - cat_name)
  2. service_sub_cats (id - cat_id - sub_cat_name)
  3. 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

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -