arielmejiadev/laravel-query-class

it creates query classes to handle easily requests with multiple filters, sorts and includes.

1.0.1 2021-07-15 06:44 UTC

This package is auto-updated.

Last update: 2024-02-28 18:41:39 UTC


README

68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f4c61726176656c2532305175657279253230436c6173732e706e673f7468656d653d6c69676874267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d417269656c4d656a69614465762532466c61726176656c2d71756572792d636c617373267061747465726e3d746963546163546f65267374796c653d7374796c655f31266465736372697074696f6e3d68656c70732b746f2b68616e646c652b72657175657374732b776974682b6d756c7469706c652b66696c746572732532432b736f7274732b616e642b696e636c75646573266d643d312673686f7757617465726d61726b3d3026666f6e7453697a653d313030707826696d616765733d68747470732533412532462532466c61726176656c2e636f6d253246696d672532466c6f676f6d61726b2e6d696e2e737667

Latest Version on Packagist Total Downloads GitHub Actions

It generates query classes to handle requests with multiple filters, sorts and includes.

The idea comes as a tip from a LaraconUS 2019 talk from Freek Van der Herten

Installation

You can install the package via composer:

composer require arielmejiadev/laravel-query-class --dev

Stubs & Commands installation

php artisan query-class:install

Usage

php artisan make:query Users/UserQuery --model=User

It would generate a class in app/Http/Queries/Users/UserQuery.php:

You can choose if your query class would extend from Spatie Query Builder (REST API STANDARD) of from an Eloquent query class (for custom cases).

Screen Shot 2021-07-12 at 23 03 53

namespace App\Http\Queries\Users;

use App\Models\User;
use Spatie\QueryBuilder\QueryBuilder;

class UserQuery extends QueryBuilder
{
    public function __construct()
    {
        parent::__construct(User::query());

        $this->allowedFilters('updated_at', 'created_at')
            ->allowedSorts('created_at')
            ->allowedIncludes('team');
    }
}

You can chain any Eloquent method or if you choose Spatie Query Builder methods from both tools.

Now you can inject the Query Class in any controller constructor and the Laravel container will resolve it by itself.

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email arielmejiadev@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.