1 Star 1 Fork 0

yiming0 / eloquent-filter

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

eloquent-filter

使用

  • 安装
composer config repo.eloquent-filter vcs "https://gitee.com/yiming0/eloquent-filter.git"
composer require ymg/eloquent-filter
  • 发布资源
php artian vendor:publish --tag=eloquent-filter-config
  • 定义过滤器
namespace App\Models\Filters;

/**
 * User Model Filter
 */
class UserFilter extends \Ymg\EloquentFilter\EloquentFilter 
{
    /**
    * User.name 字段过滤方法
    * @param $value
    * @return void
    */
    public function filterName($value)
    {
        $this->builder->where('name', $value);
    }

}

配置

  • 参考 config/eloquent-filter.php

说明

  1. 默认已开启过滤器自动注册,可以修改配置文件:"auto_register" => false

  2. 使用效果:

  • 路由:
Route::get('user', function(App\Models\User $user){
    return $user->toSql();
});
  • 请求:
GET /user?name=test
  • 结果:
select * from users where "name" = "test";
  1. 如果关闭了过滤器自动注册,可以为模型手动应用过滤器:
  • 全局 Scope:
//  App\Models\User

use Ymg\EloquentFilter\FilterScope;

// ...

    protected static function booted() 
    {
        parent::booted();
        static::addGlobalScope(app(FilterScope::class));
    }

// ...
  • 局部 Scope:
//  App\Models\User

use App\Models\Filters\UserFilter;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Ymg\EloquentFilter\EloquentFilter;

// ...
    
    public function scopeApplyFilter(Builder $builder, EloquentFilter $filter) 
    {
        $filter->resolve($builder)
    }

    public function scopeFiltered(Builder $builder, Request $request) 
    {
        UserFilter::make($request)->resolve($builder)
    }
  • 局部 Scope 使用:
// App\Http\Controllers\UserController

//...

    public function index(Request $request, User $user)
    {
        return $user->filtered($request)->get();
    }
    
    public function store(Request $request, User $user)
    {
        $request->merge(['name' => $request->input('username')]);
    
        return $user->filtered($request)->get();
    }
// App\Http\Controllers\UserController

//...

    public function index(Request $request, User $user)
    {
        return $user->applyFilter(UserFilter::make($request))->get();
    }

关于Scope,参考Laravel文档 #query-scopes

  1. 通用过滤器
  • 定义
namespace App\Models\Filters;

use Ymg\EloquentFilter\EloquentFilter;

class StatusFilter extends EloquentFilter
{
    public function filterStatus($value)
    {
        $this->builder->where('status', $value);
    }
}
  • 使用
// App\Http\Controllers\UserController

use App\Models\Filters\StatusFilter;

//...

    public function index(User $user, StatusFilter $filter)
    {
        return $user->applyFilter($filter)->get();
    }
MIT License Copyright (c) 2022 yiming0 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

暂无描述 展开 收起
PHP
MIT
取消

发行版 (3)

全部

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yiming0/eloquent-filter.git
git@gitee.com:yiming0/eloquent-filter.git
yiming0
eloquent-filter
eloquent-filter
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891