Fetching users with role admin within a LookupTableBuilder

william - 1 week ago

I have a LookupTableBuilder where i want to fetch only users with the role admin. How do we do that? Tried the below but i guess that's not the way.

$query->with(['roles'=> function ($q) {
        $q->where('slug', 'admin');
}]);

Answer

william - 1 week ago

This is the way

$query->whereHas('roles', function ($q) {
        $q->whereIn('slug', ['admin']);
});

william - 1 week ago

This is the way

$query->whereHas('roles', function ($q) {
        $q->whereIn('slug', ['admin']);
});

ryanthompson - 1 week ago

@william nicely done! I can't say I've ever done that before but looks like it runs off the existing relationship? Very nice.