I am using Laravel 5.5 with Zizaco Entrust Library. https://github.com/Zizaco/entrust
I setup the models for Role, Permission and User according to the documentation.
users Table:
+----+-------+-----------------+--+
| id | name | email | |
+----+-------+-----------------+--+
| 2 | admin | admin@admin.com | |
+----+-------+-----------------+--+
roles Table
+----+-------+
| id | name |
+----+-------+
| 1 | admin |
+----+-------+
role_user Table
+---------+---------+
| user_id | role_id |
+---------+---------+
| 2 | 1 |
+---------+---------+
However
$user = User::with('roles')->where('id', 4)->get();
Returns an empty array for 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'
];
}