|
1 | 1 | # Upgrading |
2 | 2 |
|
| 3 | +## From v6 to v7 |
| 4 | + |
| 5 | +### Requirements |
| 6 | + |
| 7 | +- PHP 8.2+ |
| 8 | +- Laravel 12 or 13 |
| 9 | + |
| 10 | +Support for Laravel 10 and 11 has been dropped. |
| 11 | + |
| 12 | +### Variadic parameters |
| 13 | + |
| 14 | +The `allowedFilters()`, `allowedSorts()`, `allowedIncludes()`, `allowedFields()`, `defaultSort()`, and `defaultSorts()` methods now accept variadic arguments instead of arrays. |
| 15 | + |
| 16 | +```php |
| 17 | +// Before |
| 18 | +QueryBuilder::for(User::class) |
| 19 | + ->allowedFilters(['name', 'email']) |
| 20 | + ->allowedSorts(['name']) |
| 21 | + ->allowedIncludes(['posts']) |
| 22 | + ->allowedFields(['id', 'name']); |
| 23 | + |
| 24 | +// After |
| 25 | +QueryBuilder::for(User::class) |
| 26 | + ->allowedFilters('name', 'email') |
| 27 | + ->allowedSorts('name') |
| 28 | + ->allowedIncludes('posts') |
| 29 | + ->allowedFields('id', 'name'); |
| 30 | +``` |
| 31 | + |
| 32 | +If you have dynamic arrays, use the spread operator: |
| 33 | + |
| 34 | +```php |
| 35 | +$filters = ['name', 'email']; |
| 36 | +QueryBuilder::for(User::class)->allowedFilters(...$filters); |
| 37 | +``` |
| 38 | + |
| 39 | +### `SortDirection` is now an enum |
| 40 | + |
| 41 | +The `SortDirection` class with string constants has been replaced with a proper PHP enum. |
| 42 | + |
| 43 | +```php |
| 44 | +// Before |
| 45 | +use Spatie\QueryBuilder\Enums\SortDirection; |
| 46 | + |
| 47 | +AllowedSort::field('name')->defaultDirection(SortDirection::DESCENDING); |
| 48 | + |
| 49 | +// After |
| 50 | +use Spatie\QueryBuilder\Enums\SortDirection; |
| 51 | + |
| 52 | +AllowedSort::field('name')->defaultDirection(SortDirection::Descending); |
| 53 | +``` |
| 54 | + |
| 55 | +The `AllowedSort::defaultDirection()` method now requires a `SortDirection` enum value instead of a string. |
| 56 | + |
| 57 | +### Filter renames |
| 58 | + |
| 59 | +`AllowedFilter::beginsWithStrict()` has been renamed to `AllowedFilter::beginsWith()`. |
| 60 | +`AllowedFilter::endsWithStrict()` has been renamed to `AllowedFilter::endsWith()`. |
| 61 | + |
| 62 | +```php |
| 63 | +// Before |
| 64 | +AllowedFilter::beginsWithStrict('name'); |
| 65 | +AllowedFilter::endsWithStrict('name'); |
| 66 | + |
| 67 | +// After |
| 68 | +AllowedFilter::beginsWith('name'); |
| 69 | +AllowedFilter::endsWith('name'); |
| 70 | +``` |
| 71 | + |
| 72 | +### `AllowedInclude` factory methods now return `self` instead of `Collection` |
| 73 | + |
| 74 | +`AllowedInclude::relationship()`, `AllowedInclude::count()`, `AllowedInclude::exists()`, `AllowedInclude::callback()`, and `AllowedInclude::custom()` now return a single `AllowedInclude` instance instead of a `Collection`. This should not affect most usage since you typically pass them to `allowedIncludes()`. |
| 75 | + |
| 76 | +### Static delimiter methods removed |
| 77 | + |
| 78 | +The static delimiter methods on `QueryBuilderRequest` have been removed (`setArrayValueDelimiter`, `setFilterArrayValueDelimiter`, `setSortsArrayValueDelimiter`, `setIncludesArrayValueDelimiter`, `setFieldsArrayValueDelimiter`, `setAppendsArrayValueDelimiter`, `resetDelimiters`). |
| 79 | + |
| 80 | +Delimiters are now configured via the `delimiter` key in the config file: |
| 81 | + |
| 82 | +```php |
| 83 | +// config/query-builder.php |
| 84 | +'delimiter' => ',', |
| 85 | +``` |
| 86 | + |
| 87 | +The `$arrayValueDelimiter` parameter has also been removed from all `AllowedFilter` factory methods. |
| 88 | + |
| 89 | +### `allowedFields()` no longer needs to be called before `allowedIncludes()` |
| 90 | + |
| 91 | +The `AllowedFieldsMustBeCalledBeforeAllowedIncludes` exception has been removed. You can now call `allowedFields()` and `allowedIncludes()` in any order. |
| 92 | + |
| 93 | +### Config changes |
| 94 | + |
| 95 | +The `disable_invalid_includes_query_exception` config key has been renamed to `disable_invalid_include_query_exception` (singular "include"). |
| 96 | + |
| 97 | +The `convert_relation_table_name_strategy` config now uses `null` instead of `false` as its default/disabled value. |
| 98 | + |
| 99 | +A new `delimiter` config key has been added (default: `','`). |
| 100 | + |
| 101 | +### Filter interface return type |
| 102 | + |
| 103 | +The `Filter` interface's `__invoke` method now has an explicit `void` return type. If you have custom filter classes, update them: |
| 104 | + |
| 105 | +```php |
| 106 | +// Before |
| 107 | +public function __invoke(Builder $query, $value, string $property) |
| 108 | + |
| 109 | +// After |
| 110 | +public function __invoke(Builder $query, mixed $value, string $property): void |
| 111 | +``` |
| 112 | + |
| 113 | +The same applies to the `Sort` interface and `IncludeInterface`. |
| 114 | + |
| 115 | +### Filter class hierarchy refactored |
| 116 | + |
| 117 | +`FiltersPartial` no longer extends `FiltersExact`, and `FiltersOperator` no longer extends `FiltersExact`. If you were extending these classes and relying on the inheritance chain, note that relation constraint handling is now provided via the `HandlesRelationConstraints` trait in `Spatie\QueryBuilder\Filters\Concerns`. |
| 118 | + |
| 119 | +### PHPStan level raised to 6 |
| 120 | + |
| 121 | +The PHPStan analysis level has been bumped from 5 to 6. |
| 122 | + |
3 | 123 | ## From v5 to v6 |
4 | 124 |
|
5 | 125 | A lot of the query builder classes now have typed properties and method parameters. If you have any custom sorts, includes, or filters, you will need to specify the property and parameter types used. |
|
0 commit comments