stephenjude/simple-query-filter

This package is abandoned and no longer maintained. The author suggests using the spatie/laravel-query-builder package instead.

Easily filter eloquent model queries from HTTP requests

2.0.0 2022-03-04 22:03 UTC

This package is auto-updated.

Last update: 2022-08-22 15:38:28 UTC


README

This package is abandoned and no longer maintained. Consider using the spatie/laravel-query-builder package instead.

Simple Query Filter

Latest Version on Packagist GitHub Tests Action Status Total Downloads

This package allows you to filter eloquent model queries based on HTTP request.

Installation

You can install the package via composer:

composer require stephenjude/simple-query-filter

Usage

Add the WithQueryFilter trait to your searchable model:

use Stephenjude\SimpleQueryFilter\WithQueryFilter;

class Post extends Model
{
    use WithQueryFilter;
}

Filter a model based on a request: /posts?column_name=search_string:

The filter() method is used to filter the rows in a table. The result will only include rows that meets all the criteria of the query parameters.

class PostController extends Controller
{
    public function index(Request $request)
    {
        // GET /posts?title=simple&slug=simple-query-filter
        $posts = Post::filter($request->query())->latest()->paginate();
    }
}

Search a model based on a request: /posts?column_name=search_string:

The scout() method is used to perform a full search on the model. The result for this method includes any row that meets the search criteria.

class PostController extends Controller
{
    public function index(Request $request)
    {
        // GET /posts?title=simple&slug=simple-query-filter
        $posts = Post::scout($request->query())->latest()->paginate();
    }
}

Custom Query Parameters

You can alternatively pass an array of column names and search strings as a key-value pair to the filter method:

    $queryParams = [
        'title' => 'Simple',
        'description' => 'Query Filter'
    ];

    $posts = Post::filter($queryParam)->latest()->paginate();
    $posts = Post::scout($queryParam)->latest()->paginate();

Column Not Found Exception

The eloquent filter scope provided in this package will throw a bad request HTTP exception if it fails to find any of the specified column names.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

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