Skip to content
This repository has been archived by the owner on Aug 22, 2022. It is now read-only.

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

License

Notifications You must be signed in to change notification settings

stephenjude/simple-query-filter

Repository files navigation

Deprecation Notice

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.

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages