Please ask for an eloquent related query in laravel
ringa_lee
ringa_lee 2017-05-16 16:55:49
0
2
385

Table Structure

// role表
id  role_id   user_id

// permission表
id  permission_id  role_id


Now we need to based on Auth::user()->id where roleuser_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?

ringa_lee
ringa_lee

ringa_lee

reply all(2)
世界只因有你
class User extends Model {

    public function iwantpermissions()
    {
        return $this->hasMany('App\Permissions','role_id', 'role_id');
    }

}

$permissions = User::find(1)->iwantpermissions;

one to one的类似。

漂亮男人
phpclass 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();//未测试,不知是否可行
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template