tsfcorp / lister
Listing library for Laravel
Installs: 2 965
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 4
Forks: 0
Open Issues: 0
Requires
- php: >=7.1
- laravel/framework: ^5.4|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
Requires (Dev)
- orchestra/testbench: ~4.0
- phpunit/phpunit: ^8.0
README
Makes it easy to list a resource
Installation
Require this package in your composer.json
and update composer. Run the following command:
composer require tsfcorp/lister
After updating composer, the service provider will automatically be registered and enabled, along with the facade, using Auto-Discovery
Next step is to run the artisan command to bring the config into your project
php artisan vendor:publish --provider="TsfCorp\Lister\ListerServiceProvider"
Update config/lister.php
Usage Instructions
Lister
library can be added to your method signature, this way it will be resolved by DI container or it can be instatiated like this:
$lister = new Lister(request(), DB::connection());
Query settings must be specified in this format:
$query_settings = [ 'fields' => "users.*", 'body' => "FROM users {filters}", 'filters' => [ "users.id IN (1,2,3)", "users.name LIKE '%{filter_name}%'", ], 'sortables' => [ 'name' => 'asc', ], // optional, you can pass a model reference and the records returned // will be of that type 'model' => User:class, ]; $listing = $lister->make($query_settings)->get();
{filters}
keyword must be specified in thebody
parameter, so it can be replaced with the conditions specified.- each item from
filters
param, will be added toWHERE
clause. If the condition has a parameter specified in curly braces, we'll search for that parameter in the request and replace the parameter with value found.
If using remembered filters and also for query string cleanup, this is needed at the top of the method used:
if($redirect_url = $lister->getRedirectUrl()) return redirect($redirect_url);