search

Home  >  Q&A  >  body text

Please ask for an eloquent related query in laravel

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_leeringa_lee2771 days ago399

reply all(2)I'll reply

  • 世界只因有你

    世界只因有你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的类似。

    reply
    0
  • 漂亮男人

    漂亮男人2017-05-16 16:57:49

    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();//未测试,不知是否可行
    

    reply
    0
  • Cancelreply