项目作者: nahidulhasan

项目描述 :
This simple package helps you filter Eloquent data using query filters.
高级语言: PHP
项目地址: git://github.com/nahidulhasan/eloquent-filter.git
创建时间: 2019-08-31T15:18:33Z
项目社区:https://github.com/nahidulhasan/eloquent-filter

开源协议:

下载


Laravel Eloquent Filter

Latest Stable Version
Total Downloads
Latest Unstable Version
License

This simple package helps you filter Eloquent data using query filters.

Installation

Run the following command:

  1. $ composer require nahidulhasan/eloquent-filter

Getting started

Use the trait NahidulHasan\EloquentFilter\Filterable in your eloquent model.

Create a new class by extending the class NahidulHasan\EloquentFilter\QueryFilters and define your custom filters as methods with one argument. Where function names are the filter argument name and the arguments are the value.

Let’s assume you want to allow to filter articles data. Please see the following code.

  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use NahidulHasan\EloquentFilter\Filterable;
  5. class Article extends Model
  6. {
  7. use Filterable;
  8. /*
  9. * The attributes that are mass assignable.
  10. * @var array
  11. */
  12. protected $fillable = ['title', 'body'];
  13. }

Create ArticleFilter class extending QueryFilters.

  1. <?php
  2. namespace App\Filters;
  3. use Illuminate\Database\Eloquent\Builder;
  4. use NahidulHasan\EloquentFilter\QueryFilters;
  5. class ArticleFilters extends QueryFilters
  6. {
  7. /*
  8. * Filter by Title.
  9. * @param $title
  10. * @return Builder
  11. * @internal param $name
  12. * @internal param string $level
  13. */
  14. public function title($title) {
  15. return $this->builder->where('title', 'like', '%' .$title.'%');
  16. }
  17. }

With this class we can use the http query string : title=article_name or any combination of these filters. It is up to you to define if you will use AND wheres or OR.

Now in the controller you can apply these filters like as described in below :

  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Filters\ArticleFilters;
  4. use App\Models\Article;
  5. use Illuminate\Http\Request;
  6. /**
  7. * Class ArticleController
  8. * @package App\Http\Controllers
  9. */
  10. class ArticleController extends Controller
  11. {
  12. /*
  13. * Display a listing of the resource.
  14. * @param ArticleFilters $filters
  15. * @return \Illuminate\Http\Response
  16. * @internal param Request $request
  17. */
  18. public function index(ArticleFilters $filters)
  19. {
  20. $articles = Article::filter($filters)->paginate(5);
  21. return view('articles.index', compact('articles'))->with('i', (request()->input('page', 1) - 1) * 5);
  22. }
  23. }

If you go to this link you will get all code:
https://github.com/nahidulhasan/laravel-eloquent-query-filtering

Thanks to :

https://github.com/laracasts/Dedicated-Query-String-Filtering

License

Eloquent-Filter for Laravel is open-sourced software licensed under the MIT license