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

Majeed's avatar

HOW To Use where clause in egar loading eloquent Query in Laravel

I'm working on ACL system. during working on system I've face a challenge. Here is my query , How can i use where clause in this query...

$this->roles()->with('permissions')->get()->pluck('permissions')
0 likes
6 replies
Majeed's avatar

@JAHELLER -

Auth::user()->roles()->with(['permissions'=>function($q){$q->where('permissions.id','=',3);}])->get()->pluck('permissions'); 

I'm try this way but it's return empty an array.

t0berius's avatar

You don't have to use permissions.id, just use id. Try to debug the query piece by piece, to see where things go wrong.

Snapey's avatar

->pluck() is working on roles not on the permissions table

If you just want permissions then just use;

    Auth::user()
        ->roles()
        ->with('permissions'=>function($q) {
            $q->where('id',3);
        })
        ->get()
        ->permissions; 

It might be easier to tell us what you are trying to do.

Majeed's avatar

@SNAPEY - it's refers an error

** Property [permissions] does not exist on this collection **

Auth::user()->roles()->with(['permissions'=>function($q){$q->where('permission_id','=',3);}])->get()->permissions
Snapey's avatar

sorry, yes, this is a collection (an array) of roles, each will have permissions

Please or to participate in this conversation.