0

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>{{$location->id}}</li>
        <li>
         @foreach($location->member as $member)
          {{$member->id}}
         @endforeach
        </li>
    @endforeach
</ul>

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

update: The result i'm trying to achieve corresponds to this query

SELECT locations.location_id,locations.name,members.first_name,          members.last_name , locations.meters
FROM locations,members
WHERE locations.member_id = members.id

Update 2:

So i tried to access a single location with a single attached to it and that works perfectly

public function getFinalData(){
    //$locations = Location::with('member')->whereNotNull('member_id')->get();
    $location = Location::find(0);

    return view('locations.final-list',['location'=>$location]);
}

in final-list $location->member->first_name

**Update 3 ** Tinker Output for one record : enter image description here

KrisTemmerman
  • 47
  • 2
  • 10
  • member_id is a Pk of members table so how can it be NULL? You have written whereNotNull('member_id'). – Ravi Hirani Apr 25 '16 at 08:51
  • @RaviHirani Because I'm getting a list of locations dat have a member_id defined? – KrisTemmerman Apr 25 '16 at 09:16
  • I maybe missing something but your method `getFinalData` seems wrong. When you do a `with` on a model, laravel only fetches the data with respect to that relationship. It doesn't query the associated model. So in your case, when you do this `$locations = Location::with('member')->whereNotNull('member_id')->get();` you should be getting an error `Column not found`. – Dwijen Apr 25 '16 at 10:12

2 Answers2

0

In your Location model, rewrite below function:-

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

Get all locations with member detail as below:-

$locations = Location::with('member')->get();

Hope it will work for you :-)

Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42
0

For showing related tables information in blade is as shown:

Model Relations Location Model:

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

Member Model:

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

Get all locations in your controller

$locations = Location::all();
return view('locations.final-list', compact('locations'));

Or

$locations = Location::orderBy('id')->get();
return view('locations.final-list', compact('locations'));

Inside your view(blade) write the following code:

<div>
    @if($locations)
        @foreach($locations AS $location)
            <div>{{ $location->id }}</div>
            @if($location->member)
                @foreach($location->member AS $member)
                    <div>
                        {{ $member->id }}
                    </div>
                @endforeach
            @endif
        @endforeach
    @endif
</div>
Hedayatullah Sarwary
  • 2,664
  • 3
  • 24
  • 38