dwikipeddos / filterable
一个用于使用查询请求过滤Laravel模型的包
1.0.0
2022-01-19 12:56 UTC
README
一个为Laravel设计的包,旨在帮助您使用请求查询过滤模型。
安装
使用 Composer 依赖管理器安装 filterable
composer require dwikipeddos/filterable
使用示例
首先,在您的模型中需要使用 dwikipeddos/filterable
特性,这将为您模型添加一个过滤作用域
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable,Filterable;
}
然后,您需要为每个可过滤模型创建一个过滤类,并扩展 FilterBase
,您可以将此类放置在任何您想要的位置。例如,我们将其放置在 app/filters
class UserFilter extends FilterBase{
//pay attention to the function name, because the function name = the request query
public function name(string $name) : Builder {
//then all you need to do is filter the builder inside this function;
return $this->builder->name("name","LIKE","%$name%");
}
}
最后,要过滤查询,您只需要在模型中调用过滤操作!
public function index(UserFilter $filter){
return User::filter($filter)->get();
}
就这样,现在这个包将过滤掉您需要的所有查询。