Merge pull request #9995 from tiagof/tiagof-patch-1

Add option to remove default constraint operators'
This commit is contained in:
Dan Harrin
2023-12-02 21:31:37 +00:00
committed by GitHub
2 changed files with 54 additions and 0 deletions
@@ -347,6 +347,49 @@ TextConstraint::make('author')
->icon('heroicon-m-user')
```
## Overriding the default operators
Each constraint type has a set of default operators, which you can customize by using the `operators()`method:
```php
use Filament\Tables\Filters\QueryBuilder\Constraints\Operators\IsFilledOperator;
use Filament\Tables\Filters\QueryBuilder\Constraints\TextConstraint;
TextConstraint::make('author')
->relationship(name: 'author', titleAttribute: 'name')
->operators([
IsFilledOperator::make(),
])
```
This will remove all operators, and register the `EqualsOperator`.
If you'd like to add an operator to the end of the list, use `pushOperators()` instead:
```php
use Filament\Tables\Filters\QueryBuilder\Constraints\Operators\IsFilledOperator;
use Filament\Tables\Filters\QueryBuilder\Constraints\TextConstraint;
TextConstraint::make('author')
->relationship(name: 'author', titleAttribute: 'name')
->pushOperators([
IsFilledOperator::class,
])
```
If you'd like to add an operator to the start of the list, use `unshiftOperators()` instead:
```php
use Filament\Tables\Filters\QueryBuilder\Constraints\Operators\IsFilledOperator;
use Filament\Tables\Filters\QueryBuilder\Constraints\TextConstraint;
TextConstraint::make('author')
->relationship(name: 'author', titleAttribute: 'name')
->unshiftOperators([
IsFilledOperator::class,
])
```
## Creating custom constraints
Custom constraints can be created "inline" with other constraints using the `Constraint::make()` method. You should also pass an [icon](#customizing-the-constraint-icon) to the `icon()` method:
@@ -38,6 +38,17 @@ trait HasOperators
* @param array<class-string<Operator> | Operator> $operators
*/
public function operators(array $operators): static
{
$this->operators = [];
$this->pushOperators($operators);
return $this;
}
/**
* @param array<class-string<Operator> | Operator> $operators
*/
public function pushOperators(array $operators): static
{
foreach ($operators as $operator) {
if (is_string($operator)) {