I have 2 tables that have a many to many relation ship
Members: id, name , ...
Locations: id,location_id
Connected by a pivot table
members_location: member_id, location_id
in my Location model I have the following function
public function members(){
return $this->belongsToMany('App\Member','location_member','location_id','member_id');
}
LocationController.php Passes data to the blade template
$locations = Location::All();
return view('locations.overview',['locations' => $locations]);
So in my overview.blade.php i do the following
@include('includes.message-block')
<!-- get a list of locations -->
<table class="member_overview table table-striped table-hover">
<thead>
<tr>
<th>Naam</th>
<th>Locatie</th>
<th>Opmerking</th>
</tr>
</thead>
<tbody>
@foreach($locations as $location)
<tr>
<td>
{{$location->location_id}}
</td>
<td>
{{$location->members->id}}
</td>
<td>
</td>
</tr>
@endforeach
Which returns an error.
Undefined property: Illuminate\Database\Eloquent\Collection::$id (View: ...)
If i drop id property on:
<td>
{{$location->members->id}}
</td>
I get an array [{"id":1,"first_name":"Sa ..}]. how can I select the first_name property from that array
tinker output when selecting one location
$location->members => Illuminate\Database\Eloquent\Collection {#658 all: [ App\Member {#664 id: 1,