属性 [name] 在此集合实例上不存在
Property [name] does not exist on this collection instance
我在数据库中有 3 个 table(用户、区域、area_user),
用户table:
ID
姓名
用户名
密码
地区table:
ID
名字
area_usertable:
ID
user_id
area_id
我正在尝试创建一个(列出用户页面),它将显示所有用户 table 列以及用户分配的区域。
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模型文件:
<?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');
}
}
用户控制器@索引文件:
<?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]
);
}
最后 table 查看文件:-
@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
如果我在用户 table 视图(指定区域)中键入仅使用区域 属性 ($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
请注意,如果我在上面的视图中指定区域关系中的列名称 ($u->areas->name),错误显示:-
属性 [名称] 在此集合实例上不存在。 (查看:C:\xampp\htdocs
如何在视图文件中只显示 (areas.name) 列
由于是多对多关系,不能只显示区域属性。您需要遍历区域:
@foreach($users as $u)
@foreach ($u->areas as $area)
{{ $area->name }}
@endforeach
@endforeach
因为 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
正如所有提到的答案,areas
是一个集合。
您可以将名称与 <td>{{ $user->areas->pluck('name')->implode(' ') }}</td>
连接起来
我在数据库中有 3 个 table(用户、区域、area_user),
用户table: ID 姓名 用户名 密码
地区table: ID 名字
area_usertable: ID user_id area_id
我正在尝试创建一个(列出用户页面),它将显示所有用户 table 列以及用户分配的区域。
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模型文件:
<?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');
}
}
用户控制器@索引文件:
<?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]
);
}
最后 table 查看文件:-
@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
如果我在用户 table 视图(指定区域)中键入仅使用区域 属性 ($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
请注意,如果我在上面的视图中指定区域关系中的列名称 ($u->areas->name),错误显示:-
属性 [名称] 在此集合实例上不存在。 (查看:C:\xampp\htdocs
如何在视图文件中只显示 (areas.name) 列
由于是多对多关系,不能只显示区域属性。您需要遍历区域:
@foreach($users as $u)
@foreach ($u->areas as $area)
{{ $area->name }}
@endforeach
@endforeach
因为 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
正如所有提到的答案,areas
是一个集合。
您可以将名称与 <td>{{ $user->areas->pluck('name')->implode(' ') }}</td>