Skip to content

ArielMejiaDev/laravel-query-class

Repository files navigation

Laravel query class

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.

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages