jie-anthony / eloquent-filter-in-hyperf
An Eloquent way to filter Eloquent Models
2.1.0
2021-03-04 06:22 UTC
Requires
- php: >=7.2
- ext-swoole: >=4.5
- hyperf/command: ~2.0|~2.1
- hyperf/database: ~2.0|~2.1
- hyperf/devtool: ~2.0|~2.1
- hyperf/framework: ~2.0|~2.1
- hyperf/utils: ~2.0|~2.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.14
- hyperf/testing: ~2.0|~2.1
- phpstan/phpstan: ^0.12.36
- swoft/swoole-ide-helper: dev-master
README
基于tucker-eric/eloquentfilter 调整为适配Hyperf的模型查询过滤器
安装
composer require jie-anthony/eloquent-filter-in-hyperf -vvv
发布配置
php bin/hyperf.php vendor:publish jie-anthony/eloquent-filter-in-hyperf
命令行创建文件
php bin/hyperf.php gen:eloquent-filter UserFilter
Filter文件
<?php
declare (strict_types=1);
namespace App\ModelFilters;
use JieAnthony\EloquentFilter\ModelFilter;
class UserFilter extends ModelFilter
{
public function name($name)
{
return $this->where('name', 'LIKE', "$name%");
}
public function age($age)
{
return $this->where('age', $age);
}
}
模型
<?php
declare (strict_types=1);
namespace App\Model;
use Hyperf\DbConnection\Model\Model;
use JieAnthony\EloquentFilter\Filterable;
class User extends Model
{
use Filterable;
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'users';
public function modelFilter()
{
return $this->provideFilter(\App\ModelFilters\UserFilter::class);
}
}
查询
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
namespace App\Controller;
use App\Model\User;
use Hyperf\HttpServer\Annotation\AutoController;
/**
* @AutoController()
*/
class IndexController extends AbstractController
{
public function test()
{
$params = $this->request->all();
$data = User::filter($params)->get();
return [
'params' => $params,
'data' => $data
];
}
}