K
K
kaxa32012021-11-26 15:02:41
Laravel
kaxa3201, 2021-11-26 15:02:41

Wherehas the right way to write a query?

There are permissions for one of them, the name is 'test',
I want to get all the roles for which permission != test, but for some reason all are returned

$roles = UserRole::whereHas('permissions', function (Builder $query) {
                 $query->where('name', '!=',  'test');
            });

and this is how the only permission with the name test returns all the rules
$roles = UserRole::whereHas('permissions', function (Builder $query) {
                 $query->where('name', '=',  'test');
            });


I noticed that if the user has only one role, then everything works, and if there are several, then it displays all the permissions

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
kaxa3201, 2021-11-26
@kaxa3201

need to use whereDoesntHave

V
Vladislav Orlov, 2021-11-26
@haveacess

try replacing != with <>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question