简体   繁体   English

在blade模板laravel中显示相关表格信息

[英]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)这给了我以下错误:试图获取非对象的属性(视图:位置/最终列表.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:更新 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在最终列表 $location->member->first_name

**Update 3 ** Tinker Output for one record : **更新 3 ** 一条记录的 Tinker 输出: 在此处输入图片说明

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:在blade中显示相关表信息如图:

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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