简体   繁体   中英

Showing related table information in blade template laravel

I have the following relationship between my tables

关系

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 : 在此处输入图片说明

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 :-)

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM