Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

TheAkashRajput's avatar

How to handle these relations with Laravel Eloquent and Entrust roles?

I'm having following relations in my Laravel project. I'll be using entrust for role based routes. However I want to use Laravel's Eloquent relations for obvious reasons. How to declare such relations in Laravel eloquents

Class Company extends Eloquent {

public function admin(){

    return $this->hasOne('App\User');

}

public function teamLeaders(){

    return $this->hasMany('App\User');

}


public function members(){

    return $this->hasMany('App\User');

}

I wish there was a way like this:

public function members(){

    return $this->hasMany('App\User')->whereRole('member');

}
0 likes
8 replies
abusalameh's avatar
Level 30

you can it this way

public function members(){

    return $this->hasMany('App\User')->where('role','=', 'member')->get();

}

give it a shot

3 likes
abusalameh's avatar

@TheAkashRajput it depends on your needs

and I think entrust is for roles, permissions handling.

maybe it's different from the relation above because the relation above gets you back the users with role member it's not checking one user unless you specify the user_id

1 like

Please or to participate in this conversation.