Laravel / Sentinel - 如何通过 pivot 获取值 table
Laravel / Sentinel - How to get value through pivot table
我使用Sentinel Framework 进行授权和认证。但是我卡在了用户管理中。
我想显示电子邮件 - 角色等详细信息,但我仍然卡在这里。
我的数据库:
role: ID, slug, name
user: ID, Email, ...
role_users: user_id, role_id
我的控制器
public function getAll(){
$user = User::all();
return view('admin.user.list',['user'=>$user]);
}
我的用户模型
public function Role(){
return $this->belongsToMany('App\Role','role_users','user_id', 'role_id');
}
那我的看法
<?php foreach ($user as $u): ?>
<tr>
<td>{{$u->Role->name}}</td>
</tr>
<?php endforeach; ?>
然后我得到一个错误
Property [slug] does not exist on this collection instance.
如果我写
<td>{{$u->Role}}</td>
它看起来像一个数组
[{"id":1,"slug":"admin","name":"admin","permissions":null,"created_at":null,"updated_at":null,"pivot":{"user_id":1,"role_id":1}}]
但是我无法访问 "slug" 属性 :(
我该怎么做?
非常感谢!!!
用户模型
class User extends Model
{
/**
* The roles that belong to the user.
*/
public function roles()
{
return $this->belongsToMany('App\Role');
}
}
榜样
class Role extends Model
{
/**
* The users that belong to the role.
*/
public function users()
{
return $this->belongsToMany('App\User');
}
}
控制器
public function getAll(){
$user = User::all();
return view('admin.user.list',['user'=>$user]);
}
查看
@foreach ($user as $u)
// becuase many to many you need this
@foreach ($u->roles as $role)
{{$role->name}}
@endforeach
@endforeach
查看 Laravel 多对多文档以获取更多信息:Laravel Many To Many
我使用Sentinel Framework 进行授权和认证。但是我卡在了用户管理中。
我想显示电子邮件 - 角色等详细信息,但我仍然卡在这里。
我的数据库:
role: ID, slug, name
user: ID, Email, ...
role_users: user_id, role_id
我的控制器
public function getAll(){
$user = User::all();
return view('admin.user.list',['user'=>$user]);
}
我的用户模型
public function Role(){
return $this->belongsToMany('App\Role','role_users','user_id', 'role_id');
}
那我的看法
<?php foreach ($user as $u): ?>
<tr>
<td>{{$u->Role->name}}</td>
</tr>
<?php endforeach; ?>
然后我得到一个错误
Property [slug] does not exist on this collection instance.
如果我写
<td>{{$u->Role}}</td>
它看起来像一个数组
[{"id":1,"slug":"admin","name":"admin","permissions":null,"created_at":null,"updated_at":null,"pivot":{"user_id":1,"role_id":1}}]
但是我无法访问 "slug" 属性 :(
我该怎么做?
非常感谢!!!
用户模型
class User extends Model
{
/**
* The roles that belong to the user.
*/
public function roles()
{
return $this->belongsToMany('App\Role');
}
}
榜样
class Role extends Model
{
/**
* The users that belong to the role.
*/
public function users()
{
return $this->belongsToMany('App\User');
}
}
控制器
public function getAll(){
$user = User::all();
return view('admin.user.list',['user'=>$user]);
}
查看
@foreach ($user as $u)
// becuase many to many you need this
@foreach ($u->roles as $role)
{{$role->name}}
@endforeach
@endforeach
查看 Laravel 多对多文档以获取更多信息:Laravel Many To Many