krenor / eloquent-filter by Krenor

Filtering Laravel Eloquent queries with ease!
43
5
2
Package Data
Maintainer Username: Krenor
Maintainer Contact: stanislav.goldmann@gmail.com (Stanislav Goldmann)
Package Create Date: 2015-09-09
Package Last Update: 2015-09-09
Language: PHP
License: MIT
Last Refreshed: 2024-03-27 03:21:30
Package Statistics
Total Downloads: 43
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 5
Total Watchers: 2
Total Forks: 0
Total Open Issues: 1

Latest Stable Version License

eloquent-filter

Simple and easy filtering an Eloquent Query of Laravel 5.1 with Inputs!

Installation

Step 1: Install Through Composer

Add to your root composer.json and install with composer install or composer update

{
  "require": {
    "krenor/eloquent-filter": "~1.0.0"
  }
}

or use composer require krenor/eloquent-filter in your console.

Step 2: Import the Trait

In the Model you want to listen to Inputs, and automatically
filter them down by that input, just use and add the Trait.

namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model;
use Krenor\EloquentFilter\FilterableTrait;

class Order extends Model
{
	use FilterableTrait;

    ...
}

Usage

Add a protected $filterable = [] to the Model using the Trait.

  1. Column Names

    • Correspond to the column name in the URL and filter down by that.
      URL : /some_orders/all?status_id=2
      protected $filterable = [ 'status_id' ]
  2. Aliases

    1. Use an column alias instead for nice names.
      URL : /some_orders/all?status=2
      protected $filterable = [ 'status_id' => 'status' ]

    2. Add values aliases to a column alias.
      URL : /some_orders/all?status=processing

    protected $filterable = [
        // column name => column alias
        'status_id' => ['status' => [
            // value aliases in database => input value
            1 => 'pending',
            2 => 'processing',
            3 => 'completed'
        ]
    ]
    

Now each time you run a query like Order::with('relation1')->paginate()
the filter is automatically applied and checks for the Inputs in the URL.
Note that this package currently only supports the equal operator for filtering a query down.

Contributing

If you believe you have found an issue, please report it using the GitHub issue tracker,
or better yet, fork the repository and submit a pull request.

Licence

eloquent-filter is distributed under the terms of the MIT license