Problems associated with laravel model

user table and role table have many-to-many relationship, and the middle table is user_roles.

many-to-many relationships are defined in the user model class

    public function relRole(){
        return $this->belongsToMany("App\Model\Role","user_roles","user_id","role_id");
    }

get the id of role with user 2 in the userController controller class, my code

$data=AdminUser::with("relRole")->where("id",2)->get()->toArray();

the problem now: getting a lot of data, and I only want the relevant id values in the role table, how to write it, only using the model method, regardless of query constructor and native.

clipboard.png

Apr.01,2022

$data=AdminUser::with('relRole:id')->where('id',2)->get()->toArray();

  https://laravelacademy.org/po... many to many parts 

Menu