ensi / laravel-query-builder-helpers
This package contains extensions for laravel-query-builder
Installs: 18 748
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^8.1
- laravel/framework: ^9.0 || ^10.0 || ^11.0
- spatie/laravel-query-builder: ^5.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.2
- orchestra/testbench: ^7.0 || ^8.0 || ^9.0
- pestphp/pest: ^1.22 || ^2.0
- pestphp/pest-plugin-laravel: ^1.1 || ^2.0
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.11
- spaze/phpstan-disallowed-calls: ^2.15
README
The laravel-query-builder-helper package is a set of classes that simplify the creation of available filters for the laravel-query-builder package from Spatie.
Installation
You can install the package via composer:
composer require ensi/laravel-query-builder-helpers
Version Compatibility
Basic Usage
Creating a filter
Filters are created by applying the static make method and then calling the chain of filter methods.
use Ensi\QueryBuilderHelpers\Filters\StringFilter; StringFilter::make('name')->contain()->endWith()->empty();
The following filter classes are currently available:
- StringFilter
- NumericFilter
- DateFilter
- PlugFilter (a stub for passing additional parameters to another filter)
- ExtraFilter (described in the section "Additional filters")
Each filter type has its own suffix, which is added to the name passed to the make method. For example, by default, the filter empty has the suffix _empty, and the filter gt is _gt:
NumericFilter::make('rank')->exact()->empty()->gt()->lt();
As a result, we will get four filtering options available for search queries.
- rank
- rank_empty
- rank_gt
- rank_lt
Passing filters to the allowed Filters method
To transfer the received filters to the allowedFilters
method of the Spatie package, the array will need to be destructured.
$this->allowedFilters([ ...NumericFilter::make('rank')->exact()->empty()->gt()->lt(), ]);
Additional filters
The ExtraFilter class is used by the aforementioned classes, but can also be used separately.
Useful methods include:
- nested (registers a set of nested filters)
- predefined (creates a predefined filter that includes complex filtering)
- and others
...ExtraFilter::nested('page', [ ...InputFilter::make('title', 'page_title')->empty()->exact()->contain(), ]),
Configuration
In the file **config.php ** you can customize the applied suffixes. You can also set up the like operator used in search queries there.
'suffixes' => [ 'equal' => '', 'greater' => '_gt', 'less' => '_lt', ... ], 'like_operator' => 'LIKE',
Contributing
Please see CONTRIBUTING for details.
Testing
- composer install
- composer test
By default, testing takes place using in-memory DataBase SQLite.
SQLite does not support some functions, for example: json_contains.
To test these functions, copy phpunit.xml.dist
to phpunit.xml
and specify the configuration with connection to another database in the php section.
When writing such tests, use the skip function to skip tests using the default connection.
->skip(fn () => DB::getDriverName() === 'sqlite', 'db driver does not support this test');
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
License
The MIT License (MIT). Please see License File for more information.