我正在使用带有Zizaco Entrust库的Laravel 5.5。 https://github.com/Zizaco/entrust
我根据文档设置了角色,权限和用户模型。
用户表:
+----+-------+-----------------+--+
| id | name | email | |
+----+-------+-----------------+--+
| 2 | admin | admin@admin.com | |
+----+-------+-----------------+--+
角色表
+----+-------+
| id | name |
+----+-------+
| 1 | admin |
+----+-------+
role_user表
+---------+---------+
| user_id | role_id |
+---------+---------+
| 2 | 1 |
+---------+---------+
但是
$user = User::with('roles')->where('id', 4)->get();
为 roles
[
{
"id": 2,
"name": "Admin",
"email": "admin@admin.com",
"created_at": "2018-11-07 11:11:24",
"updated_at": "2018-11-07 11:24:51",
"deleted_at": null,
"company_id": 1,
"active": true,
"roles": []
}
]
User.php
class User extends Authenticatable
{
use EntrustUserTrait;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'company_id', 'active'
];
protected $casts = [
'active' => 'boolean',
];
protected $hidden = [
'password', 'remember_token'
];
}