laravel 此集合实例上不存在属性 [名称]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46846225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Property [name] does not exist on this collection instance
提问by Q8root
I have 3 tables in DB (users , areas , area_user),
我在数据库中有 3 个表(用户、区域、区域用户),
Users table: id name username password
用户表:id 名称 用户名 密码
areas table: id name
区域表:id 名称
area_user table: id user_id area_id
area_user 表: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
User.php 模型文件
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User 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('App\Area','area_user');
}
Area.php Model file:
Area.php 模型文件:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Area extends Model{
protected $fillable = ['name'];
public function users(){
return $this->belongsToMany('App\User','area_user','area_id','user_id');
}
}
UserController@index file :
UserController@index 文件:
<?php
namespace App\Http\Controllers;
use App\Areas;
use App\Roles;
use App\User;
use Illuminate\Http\Request;
class UserController extends Controller{
public function __construct(){
// $this->middleware('auth');
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
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:-
如果我只使用区域属性($u->areas)在用户表视图(分配的区域)中输入,它将显示所有区域列:-
[
{
"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:-
请注意,如果我在上述视图 ($u->areas->name) 中指定区域关系中的列名,则会显示错误:-
Property [name] does not exist on this collection instance. (View: C:\xampp\htdocs
此集合实例上不存在属性 [名称]。(查看:C:\xampp\htdocs
How can i show only the (areas.name) column in view file
如何在视图文件中仅显示 (areas.name) 列
回答by Maraboc
Because the areas
is a collection so you should loop over it like this :
因为它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
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
回答by Alexey Mezenin
Since it's a many-to-many relationship, you can't just display areas property. You need to iterate over areas:
由于它是多对多关系,您不能只显示区域属性。您需要遍历区域:
@foreach($users as $u)
@foreach ($u->areas as $area)
{{ $area->name }}
@endforeach
@endforeach
回答by Lloople
As all the answers mentioned, areas
is a collection.
正如所有提到的答案,areas
是一个集合。
You can concatenate the names with <td>{{ $user->areas->pluck('name')->implode(' ') }}</td>
您可以将名称与 <td>{{ $user->areas->pluck('name')->implode(' ') }}</td>