dimanche 29 octobre 2017

Laravel/Eloquent conditions on subproperties don't apply

I have the following models:

  • Client
    • belongsToMany(Group)
  • Group
    • belongsToMany(Client)

What I wanted is an array of all groups, where each group contains the clients belonging to it.

I was able to achieve this with the following code:

Group::with('clients')->get();

Now I want to only get the groups and clients where the client's status property is 1. So if there are multiple clients in a group, then the result should not contain clients with a status !=1. Additionally, if this condition returns 0 clients for a group, the group should also NOT be contained in the final result.

What I tried so far:

Group::with(['clients' => function($query) {
  $query->where('clients.status', '!=', 1);
}])->whereHas('clients', function($query) {
  $query->where('clients.status', '!=', 1);
})->get();

Somehow, none of both where-conditions apply when I execute the query. What am I missing or doing wrong?



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

Aucun commentaire:

Enregistrer un commentaire