mardi 18 juillet 2017

Laravel - $query->where all the ids in an are matched

I am working on an API endpoint that returns a list of users that have all of the given services ID's.

In my case:

  • Users can have many services
  • Tables: 'users', 'services', 'service_user'

I am passing an array via Vue JS to my end point for example:

/endpoint/32,35,38

My query is currently:

$servicesArray = explode(',', $services);

$users = User::whereHas('services', function ($query) use ($servicesArray) {
            foreach ($servicesArray as $key => $value) {
                $query->where('id', $value);
            }
        })
        ->get();

The issue is that it seems to return now results, even if a user does have the correct services. My relationship is fine, and if I only pass one service to the endpoint it correctly returns a user that has that service assigned. I used whereIn before, but I need to only show users that have ALL of the services specified in the endpoint array.

Any obvious reason why what I have is not working as expected?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2ve2TC0
via IFTTT

Aucun commentaire:

Enregistrer un commentaire