Table Structure
// role表
id role_id user_id
// permission表
id permission_id role_id
Now we need to based on Auth::user()->id
where role
user_id
in the table and then based on role_id
Query the permission_id
list in the permission
table. . . How to write this using eloquent
?
世界只因有你2017-05-16 16:57:49
class User extends Model {
public function iwantpermissions()
{
return $this->hasMany('App\Permissions','role_id', 'role_id');
}
}
$permissions = User::find(1)->iwantpermissions;
one to one的类似。
漂亮男人2017-05-16 16:57:49
php
class User extends Model { public function role() { return $this->hasOne('App\Role','user_id', 'id'); } } class Role extends Model { public function permission() { return $this->hasMany('App\Permission','role_id', 'role_id'); } } $permissions = User::find(Auth::user()->id)->Role()->permission();//未测试,不知是否可行