tsfcorp/lister

Listing library for Laravel

v1.0.2 2024-09-05 08:14 UTC

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 the body parameter, so it can be replaced with the conditions specified.
  • each item from filters param, will be added to WHERE 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);