Easily apply search criteria on Laravel 5 queries
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


Easily apply search criteria on Laravel 5 queries

This package will try to solve the problem with filtering a result set and will apply all the necessary checks for you.


  • Composer installable
  • PSR4 auto loading
  • Filter query in Laravel 5


Build only for Laravel Framework 5 only!


In terminal

composer require aginev/search-filters:1.0.*

Add Service Provider to your config/app.php like so

// config/app.php

'providers' => [

Publish config

php artisan vendor:publish --provider="Aginev\SearchFilters\SearchFiltersServiceProvider" --tag="config"


Add Filterable trait to you models like so

use Aginev\SearchFilters\Filterable;

Implement setFilters method and define your filters. All filter methods are accepting a database column name as parameter.

 * Set query filters
 * Overwrite this method in the model to set query filters
public function setFilters()
    $this->filter->equal('id', function ($by, $dir, $query) {
            // Every filter can recieve as last paramenter a closure that can be used for custom query order if required
            $query->orderBy($by, $dir);

Need a custom filter?

public function setFilters()
        ->custom('full_name', function ($query, $key, $value) {
            $query->where(\DB::raw("CONCAT_WS(' ', first_name, middle_name, last_name)"), 'LIKE', '%' . $value . '%');
        }, function ($by, $dir, $query) {
            // Define custom order or skip this parameter in method call
            $query->orderBy($by, $dir);

Note that you need to pass the input filters to filter scope. My URLs typically looks like this http://ex.com/?f[first_name]=Atanas&f[last_name]=GinevRetrieve Than you can get the results like so:

// Simple filter query
$users = User::filter(\Request::input('f', []))->get();

// Add additional where and pagination
$users = User::where('is_admin', '=', 1)
    ->filter(\Request::input('f', []))

Available filter methods

public function setFilters()
     ->custom('column', function($query, $column, $value) {
        // $query - instance of Illuminate\Database\Eloquent\Builder
        // $column - the string passed as first argument
        // $value - the filter value if exists and not empty
     }, function ($by, $dir, $query) {
        // Not required and can be applied to any other filter method
        // $by - order by field
        // $dir - order direction
        // $query - instance of Illuminate\Database\Eloquent\Builder
     ->equal('column')                  // column = filter_value
     ->distinct('column')               // column <> filter_value
     ->greaterThan('column')            // column > filter_value
     ->greaterOrEqualThan('column')     // column >= filter_value
     ->lessThan('column')               // column < filter_value
     ->lessOrEqualThan('column')        // column <= filter_value
     ->like('column')                   // column LIKE '%filter_value%'
     ->llike('column')                  // column LIKE '%filter_value'
     ->rlike('column')                  // column LIKE 'filter_value%'
     ->between('column')                // column BETWEEN filter_value[0] AND filter_value[1]
     ->notBetween('column')             // column NOT BETWEEN filter_value[0] AND filter_value[1]
     ->in('column')                     // column IN (filter_value[0], ..., filter_value[N])
     ->notIn('column')                  // column NOT IN (filter_value[0], ..., filter_value[N])
     ->null('column')                   // column IS NULL
     ->notNull('column')                // column IS NOT NULL
     ->date('column')                   // column DATE(column) = filter_value
     ->dateBetween('column');           // column DATE(column) BETWEEN filter_value[0] AND filter_value[1]


Want to share your custom filter methods? Submit a pull request and I'll consider them :)


MIT - http://opensource.org/licenses/MIT


Need a freelance web developer? Contact me at my website http://aginev.com