【发布时间】:2019-02-13 17:28:23
【问题描述】:
我正在尝试根据刀片中的相应数据将多个表中的字段显示到单个 html 表中。但我没有得到任何结果
我的数据库设计:
District
| id
| district_name
Municipal
| id
| district_id
| Municipal_uid
| municipal_name
Area
| id
| area_name
| district_id
| municipal_id
这就是我想要达到的目标,
Area ID | Area Name | District Name | Municipal Name | municipal UID
我的模特
地区:
public function districts(){
return $this->belongsTo('App\Districts');
}
public function municipals(){
return $this->belongsTo('App\Municipals');
}
市政:
public function district(){
return $this->belongsTo('App\Districts');
}
public function approvedlayout(){
return $this->hasMany('App\Approvedlayouts');
}
地区:
public function municipal(){
return $this->hasMany('App\Municipals');
}
public function approvedlayout(){
return $this->hasMany('App\Approvedlayouts');
}
刀片
<table class="table table-striped">
<thead class="text-center">
<tr>
<th>Area ID</th>
<th>Area Name</th>
<th>District Name </th>
<th>Municipal Name</th>
<th>Municipal UID</th>
</tr>
</thead>
<tbody class="list">
@foreach ($areas as $layout)
<tr>
<td>{{$layout ->id}}</td>
<td> {{ $layout-area_name }}</td>
<td> {{ $layout->districts-> district_name }}</td>
<td> </td>
<td></td>
<td></td>
</tr>
@endforeach
</tbody>
</table>
{{$areas-> links()}}
控制器
public function index()
{
$areas = Area::simplePaginate(5);
return view('admin.area.index',compact('areas'));
}
当我尝试显示区名时
( {{ $layout->districts-> District_name }})
我遇到了错误,
试图获取非对象的属性“区名”(查看: lapp/resources/views/area/index.blade.php
【问题讨论】:
标签: php database laravel eloquent