基于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
<?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
];
}
}