dimanche 3 décembre 2017

Laravel filter relation with where returns all

I try to filter my users by role in a subquery but returns all records

here I have a normal Model query which I extend with a subquery if the request contains the position parameter

$query = User::select()-> ....

and here comes the extension on $request->has('position')

if ($request->has('position')) {
            $role = $request->get('position');

            if ($role == 'all') {
                $query->with('roles');
            } else {
                $query->with(['roles' => function($query) use($role) {
                     $query->where('id', $role);
                }]);
            }
        } else {
            $query->with('roles');
        }

but this one returns the filtered and plus other users



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire