spatie / laravel-query-builder

Easily build Eloquent queries from API requests

Home Page:https://spatie.be/docs/laravel-query-builder

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Scope filters don't work with relationships

chimit opened this issue · comments

Scope filters allow you to add local scopes to your query by adding filters to the URL. This works for scopes on the queried model or its relationships using dot-notation.

Unlike exact filters scope filters don't work on related models.

Works:

// District
->allowedFilters([
    AllowedFilter::exact('buildings.active')->default(true),
])

Doesn't work:

// District
->allowedFilters([
    AllowedFilter::scope('buildings.active')->default(true),
])

Scope filter works well on the model itself:

// Building
->allowedFilters([
    AllowedFilter::scope('active')->default(true),
])

Building scope:

public function scopeActive($query)
{
    return $query->where('active', 1);
}

Sorry, I was wrong. I misused the scopes.