此集合实例上不存在属性 [名称]
我在数据库中有 3 个表(用户、区域、区域用户),
用户表:ID名称用户名密码
区域表:ID姓名
area_user 表:ID用户身份area_id
我正在尝试创建一个(列出用户页面),它将显示所有用户表列以及用户分配的区域.
User.php 模型文件
belongsToMany('AppArea','area_user');}
Area.php 模型文件:
belongsToMany('AppUser','area_user','area_id','user_id');}}
UserController@index 文件:
middleware('auth');}/*** 显示资源列表.** @return IlluminateHttpResponse*/公共函数索引(){$users = User::get();return view('users._list',['用户'=>$用户]);}
最后是表格视图文件:-
@foreach($users as $u)<tr role="row" class="odd"><td class="sorting_1">{{$u->name}}</td><td></td><td>{{$u->areas]}}</td><td>{{route('show_user', ['id' => $u->id])}}</td></tr></tbody>@endforeach
如果我只使用区域属性 ($u->areas) 在用户表视图(指定区域)中输入,它将显示所有区域列:-
<预><代码>[{身份证":3,"name": "C",location_id":1,created_at":空,updated_at":空,枢轴":{"user_id": 4,area_id":3}},{身份证":4,"name": "D",location_id":2,created_at":空,updated_at":空,枢轴":{"user_id": 4,area_id":4}}]<小时>
@foreach($users as $u)<tr role="row" class="odd"><td class="sorting_1">{{$u->name}}</td><td></td><td>{{$u->areas->name]}}</td><td>{{route('show_user', ['id' => $u->id])}}</td></tr></tbody>@endforeach
请注意,如果我在上面的视图 ($u->areas->name) 中指定区域关系中的列名,则显示错误:-
此集合实例上不存在属性 [名称].(查看:C:xampphtdocs
如何在视图文件中只显示(areas.name)列
解决方案因为 areas
是一个集合,所以你应该像这样循环:
@foreach($users as $u)<tr role="row" class="odd"><td class="sorting_1">{{$u->name}}</td><td></td><td>@foreach($u->areas as $area){{$area->name}},@endforeach</td><td>{{route('show_user', ['id' => $u->id])}}</td></tr></tbody>@endforeach
如果你想用逗号分隔它们,你可以这样做而无需循环:
@foreach($users as $u)<tr role="row" class="odd"><td class="sorting_1">{{$u->name}}</td><td></td><td>{{ $u->areas->pluck('name')->implode(', ') }}</td><td>{{route('show_user', ['id' => $u->id])}}</td></tr></tbody>@endforeach
I have 3 tables in DB (users , areas , area_user),
Users table: id name username password
areas table: id name
area_user table: id user_id area_id
I am trying to create a (List users page) which will show all user table column with user assigned areas.
User.php Model file
<?php
namespace App;
use IlluminateNotificationsNotifiable;
use IlluminateFoundationAuthUser as Authenticatable;
class User extends Authenticatable{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'email', 'password','role_id',];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = ['password', 'remember_token',];
public function areas(){
return $this->belongsToMany('AppArea','area_user');
}
Area.php Model file:
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class Area extends Model{
protected $fillable = ['name'];
public function users(){
return $this->belongsToMany('AppUser','area_user','area_id','user_id');
}
}
UserController@index file :
<?php
namespace AppHttpControllers;
use AppAreas;
use AppRoles;
use AppUser;
use IlluminateHttpRequest;
class UserController extends Controller{
public function __construct(){
// $this->middleware('auth');
}
/**
* Display a listing of the resource.
*
* @return IlluminateHttpResponse
*/
public function index(){
$users = User::get();
return view('users._list',
['users'=> $users]
);
}
Finally the table view file:-
@foreach($users as $u)
<tr role="row" class="odd">
<td class="sorting_1">{{$u->name}}</td>
<td></td>
<td>{{$u->areas]}}</td>
<td>{{route('show_user', ['id' => $u->id])}}</td>
</tr></tbody>
@endforeach
If i typed in user table view (assigned areas) with only using the areas property ($u->areas), it will show all the areas column:-
[
{
"id": 3,
"name": "C",
"location_id": 1,
"created_at": null,
"updated_at": null,
"pivot": {
"user_id": 4,
"area_id": 3
}
},
{
"id": 4,
"name": "D",
"location_id": 2,
"created_at": null,
"updated_at": null,
"pivot": {
"user_id": 4,
"area_id": 4
}
}
]
@foreach($users as $u)
<tr role="row" class="odd">
<td class="sorting_1">{{$u->name}}</td>
<td></td>
<td>{{$u->areas->name]}}</td>
<td>{{route('show_user', ['id' => $u->id])}}</td>
</tr></tbody>
@endforeach
Notice that if i specify the column name in Areas relationship in the above view ($u->areas->name) , error showing:-
Property [name] does not exist on this collection instance. (View: C:xampphtdocs
How can i show only the (areas.name) column in view file
解决方案Because the areas
is a collection so you should loop over it like this :
@foreach($users as $u)
<tr role="row" class="odd">
<td class="sorting_1">{{$u->name}}</td>
<td></td>
<td>
@foreach($u->areas as $area)
{{$area->name}},
@endforeach
</td>
<td>{{route('show_user', ['id' => $u->id])}}</td>
</tr></tbody>
@endforeach
And if you want to separate them whith comma for example you can do it like that without looping :
@foreach($users as $u)
<tr role="row" class="odd">
<td class="sorting_1">{{$u->name}}</td>
<td></td>
<td>
{{ $u->areas->pluck('name')->implode(', ') }}
</td>
<td>{{route('show_user', ['id' => $u->id])}}</td>
</tr></tbody>
@endforeach
相关文章