cms icon indicating copy to clipboard operation
cms copied to clipboard

[5.x] Apply scopes to query builders

Open aerni opened this issue 3 years ago • 1 comments

This PR implements the new query scopes approach as discussed in PR https://github.com/statamic/cms/pull/5869.

Each scope in the App\Scopes namespace is now also available to the query builders.

Example

Let's assume we've got a App\Scopes\PaymentSuccessful scope class. This scope can now be used in Antlers and in PHP.

// Antlers
{{ collection:blog filter="payment_successful" }}

// PHP
Entry::query()->paymentSuccessful()

The query method corresponds to the scope's handle in camel case.

// The handle in App\Scopes\PaymentSuccessful
protected static $handle = 'where_something';

Entry::query()->whereSomething()

By default, each scope is available to all query builders. You may want to restrict the scope to be available on certain query builders only. You can do that by adding the alias of the query builder to the $builders property.

// The scope is only available to the EntryQueryBuilder
protected static $builders = ['entries'];

I also added some nice exceptions. But you might decide to remove them for a more graceful handling if the scope doesn't exist.

aerni avatar Apr 27 '22 12:04 aerni

I added a bunch of tests. The only thing I wasn't sure about was how to test the eloquent builder for entries and users?

aerni avatar Apr 28 '22 13:04 aerni