you can it this way
public function members(){
return $this->hasMany('App\User')->where('role','=', 'member')->get();
}
give it a shot
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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');
}
you can it this way
public function members(){
return $this->hasMany('App\User')->where('role','=', 'member')->get();
}
give it a shot
Please or to participate in this conversation.