nahidulhasan/eloquent-filter 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

nahidulhasan/eloquent-filter

Composer 安装命令:

composer require nahidulhasan/eloquent-filter

包简介

Laravel eloquent query filter

README 文档

README

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:

$ 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.

<?php  

namespace App\Models;

use Illuminate\Database\Eloquent\Model;  
use NahidulHasan\EloquentFilter\Filterable;  
  
class Article extends Model  
{  
	use Filterable; 
	 
	/*
	* The attributes that are mass assignable. 
	*  @var array 
	*/ 
	protected $fillable = ['title', 'body'];
}  
  

Create ArticleFilter class extending QueryFilters.

<?php

namespace App\Filters;  
  
use Illuminate\Database\Eloquent\Builder;  
use NahidulHasan\EloquentFilter\QueryFilters;  
  
class ArticleFilters extends QueryFilters  
{  
  	/*  
	* Filter by Title. 
	* @param $title 
	* @return Builder 
	* @internal param $name 
	* @internal param string $level 
	*/ 
	public function title($title) { 
		return $this->builder->where('title', 'like', '%' .$title.'%'); 
	}
}  

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 :

<?php

namespace App\Http\Controllers;  
  
use App\Filters\ArticleFilters;  
use App\Models\Article;  
use Illuminate\Http\Request;  
  
/**  
* Class ArticleController 
* @package App\Http\Controllers 
*/
class ArticleController extends Controller  
{  
	/* 
	* Display a listing of the resource. 
	*  @param ArticleFilters $filters 
	*  @return \Illuminate\Http\Response 
	*  @internal param Request $request 
	*/ 
	public function index(ArticleFilters $filters) 
	{  
		$articles = Article::filter($filters)->paginate(5);  
		
		return view('articles.index', compact('articles'))->with('i', (request()->input('page', 1) - 1) * 5); 
	}
}    

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

统计信息

  • 总下载量: 303
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 25
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 25
  • Watchers: 4
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-08-31

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固