jeudi 3 août 2017

Laravel sub query not removing results when using a nested with

I have a 'user' table that has a pivot table for services that a user offers:

// App\User
public function services()
{
    return $this->hasMany('App\ServiceUser');
}

On the ServiceUser model I then have another relationship to get the service information:

public function service() 
{
    return $this->hasOne('App\Service', 'id');
}

When fetching a team (using Laravel Spark) the query I am using is:

Team::with('users')->withUserCustomerServices()->where('id', $teamId)->first();

The scope for this query is in the Team model:

public function scopeWithCustomerServices($query)
{
    $query = $query;
    $query->with('users.services');
    $query->with(['users.services.service' => function($q) {
        $q->where('visible_to_customers', 1);
    }]);
    return $query;
}

When outputting (using Vue.js):



I get (in this example) 6 results returned. However, one of the services has a database field 'visible_to_customers' set to 0.

Initially I thought my query would work as expected and only return 5 services however it actually still returns them all, but doesn't return the relationship (service) if the field is 0.

How can I can I only return the pivot table result where the relationship has a certain field value?



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

Aucun commentaire:

Enregistrer un commentaire