lundi 25 avril 2016

Showing related table information in blade template laravel

I have the following relationship between my tables

relation

In my Models i have the following code:

Location Model:

public function member() {
  return $this->belongsTo('App\Member','member_id');
 }

Member Model:

public function locations(){
    return $this->hasMany('App\Location','member_id');
}

Now in my Location controller I created the following function.

public function getFinalData(){
    $locations = Location::with('member')->whereNotNull('member_id')->get();
    return view('locations.final-list',['locations'=>$locations]);
}

In my blade template however, I am unable to iterate through the member properties

<ul>
    @foreach($locations as $location)
      <li></li>
        <li>
         @foreach($location->member as $member)
          
         @endforeach
        </li>
    @endforeach
</ul>

This gives me the following error: Trying to get property of non-object (View:locations/final-list.blade.php)



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire