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

FounderStartup's avatar

How to use laratrust roles in where condition ?

I need to show only records in which 'senderid' user has role('broker'). How can I use this condition in the controller ? I am using laratrust for roles management.

        $enquiries = BusinessLead::where('receiverid', $id)->where('status',1)->orderBy('id','DESC')->paginate(25);

0 likes
4 replies
MichalOravec's avatar
$enquiries = BusinessLead::where('receiverid', $id)
    ->where('status', 1)
    ->whereHas('sender', function ($query) {
        $query->hasRole('broker');
    })
    ->orderByDesc('id')
    ->paginate(25);

I assume sender is an relationship to user by senderid.

1 like
FounderStartup's avatar

@MichalOravec Thanks for your time. I am getting the following error :

Call to undefined method Illuminate\Database\Eloquent\Builder::hasRole()

My model :

class BusinessLead extends Model
{
    use HasFactory;
    protected $guarded = [];

    public function sender(){
        return $this->belongsTo(User::class,'senderid','id');
    }
}

Please or to participate in this conversation.