請閱讀 https://github.com/Tucker-Eric/EloquentFilter , 裏面有很全的文檔和註釋,如下僅列出關鍵部分。php
1. 安裝git
composer require tucker-eric/eloquentfilter
2. 添加Provider github
在 config/app.php 中添加 EloquentFilter\ServiceProvider::classsql
'providers' => [ // Other service providers... EloquentFilter\ServiceProvider::class, ],
3. 添加配置 數組
php artisan vendor:publish --provider="EloquentFilter\ServiceProvider"
在 config 下出現 eloquentfilter.php 。 app
注意配置文件中 命名空間配置composer
'namespace' => "App\\ModelFilters\\",
默認會讀取該命名空間下類,若使用其餘命名空間類,須要修改該配置,或者傳入該類 。 ide
//使用默認命名空間
Post::filter(['title'=>'23'])->get();
//指定類
$userFilter = App\ModelFilters\User\UserFilter::class;
User::filter($input, $userFilter)->get();
,或者在Model 中指定(下面會提到)。ui
4. 修改Modelthis
以User 爲例
namespace App;
//必須引入 use EloquentFilter\Filterable; use Illuminate\Database\Eloquent\Model; class User extends Model {
//使用Trait , 其中有 scopeFilter, 以即可以靜態調用, User::filter use Filterable;
//指定使用的過濾類 public function modelFilter() { return $this->provideFilter(App\ModelFilters\User\UserFilter::class); } //User Class }
5. 使用
5.1 動態過濾
namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; use App\User; use App\ModelFilters\Admin\UserFilter as AdminFilter; use App\ModelFilters\User\UserFilter as BasicUserFilter;
// 若沒有添加alias , 參考 github 上文檔會找不到類 use Illuminate\Support\Facades\Auth; class UserController extends Controller { public function index(Request $request) { $userFilter = Auth::user()->isAdmin() ? AdminFilter::class : BasicUserFilter::class; return User::filter($request->all(), $userFilter)->get(); } }
文檔中提升了不少功能和配置,這裏再也不復述,有不對的地方請指正!
補充:
1. User 表結構
CREATE TABLE `users` ( `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, `email` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, `phone` varchar(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, `password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, `remember_token` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, `created_at` timestamp(0) NULL DEFAULT NULL, `updated_at` timestamp(0) NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 10 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Compact;
2. Filterable 源碼
trait Filterable { /** * Creates local scope to run the filter. * * @param $query * @param array $input * @param null|string|ModelFilter $filter * @return \Illuminate\Database\Eloquent\Builder */ public function scopeFilter($query, array $input = [], $filter = null) { // Resolve the current Model's filter if ($filter === null) { $filter = $this->getModelFilterClass(); } // Create the model filter instance $modelFilter = new $filter($query, $input); // Set the input that was used in the filter (this will exclude empty strings) $this->filtered = $modelFilter->input(); // Return the filter query return $modelFilter->handle(); } }
3. UserFilter 代碼
use EloquentFilter\ModelFilter; class UserFilter extends ModelFilter {
/**
* 關聯查詢中使用 * Related Models that have ModelFilters as well as the method on the ModelFilter * As [relationMethod => [input_key1, input_key2]]. * 具備ModelFilters的相關模型以及ModelFilter上的方法 如[relationMethod => [input_key1,input_key2]]。
* @var array */ public $relations = [];
// 黑名單
// 過濾器不會調用blackist數組中定義的任何方法。這些方法一般用於內部過濾器邏輯
protected $blacklist = ['secretMethod'];
/**
* 過濾是以表字段-方法,作了映射,
*/
// User 表中字段 email public function email($email) { return $this->where('email', '=', $email); } // User 表中字段 public function name($name) { return $this->where(function($q) use ($name) { return $q->where('name', 'LIKE', "%$name%"); }); } // User 表中字段名 public function phone($phone) { return $this->where('phone', 'LIKE', "%$phone%"); } //每次filter,都會調用,非必需(參考 ModelFilter->filter ) public function setup() { $this->onlyShowDeletedForAdmins();
//$this->xxx(); } public function onlyShowDeletedForAdmins() { if(Auth::user()->isAdmin()) { //$this->withTrashed(); } } // public function secretMethod($secretParameter) { return $this->where('some_column', true); }
4. 父類 EloquentFilter\ModelFilter
use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Database\Eloquent\Builder as QueryBuilder; /** * @mixin QueryBuilder */ abstract class ModelFilter { /** * ModelFilter constructor. * * @param $query * @param array $input * @param bool $relationsEnabled */ public function __construct($query, array $input = [], $relationsEnabled = true) { $this->query = $query; $this->input = $this->removeEmptyInput($input); $this->relationsEnabled = $relationsEnabled; $this->registerMacros(); } /** * @param $method * @param $args * @return mixed */ public function __call($method, $args) { $resp = call_user_func_array([$this->query, $method], $args); // Only return $this if query builder is returned // We don't want to make actions to the builder unreachable return $resp instanceof QueryBuilder ? $this : $resp; } /** * Handle all filters. * * @return QueryBuilder */ public function handle() { // Filter global methods if (method_exists($this, 'setup')) { $this->setup(); } // Run input filters $this->filterInput(); // Set up all the whereHas and joins constraints $this->filterRelations(); return $this->query; } }