千家信息网

Pipeline如何处理Laravel多条件查询

发表于:2026-11-04 作者:千家信息网编辑
千家信息网最后更新 2026年11月04日,本篇内容介绍了"Pipeline如何处理Laravel多条件查询"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够
千家信息网最后更新 2026年11月04日Pipeline如何处理Laravel多条件查询

本篇内容介绍了"Pipeline如何处理Laravel多条件查询"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

pipeline是Laravel 特别有用的特性之一。 pipeline也是 Laravel 中最常用的组件之一,例如中间件。

One of the features of Laravel which surely useful is the pipeline. Pipelines is one of the most used components in the Laravel for example middleware.

基本上,通过管道,我们可以通过任务堆栈传递对象,并通过回调获得结果。

Basically, with a pipeline we can pass an object through a stack of tasks and get the result via a callback.

管道用于查询过滤的好处是我们可以将成吨的屎山减少到几行。 没用管道之前,我们通常会写一个控制器,获取用户模型的 Eloquent 实例,并根据查询字符串拼接一些条件。

The benefit of pipeline for query filtering is that we can reduce tons of lines to several lines. Being unaware of the pipelines, we would usually set up a controller, get an instance of Eloquent of User model, and apply some condition based on query string.

让我们看看下面的屎山查询大法。

Let's see below queries.

$query = User::query();if ($request->username) {    $query->where('username', 'LIKE', "%$request->username%");}if ($request->email) {    $query->where('email', 'LIKE', "%$request->email%");}if ($request->address) {    $query->where('address', 'LIKE', "%$request->address%");}if ($request->occupation) {    $query->where('occupation', 'LIKE', "%$request->occupation%");}return $query->get();

缺点很明显,过滤条件像屎山一样不断的堆加,出现大量重复的代码。 另外,代码的可维护性就有点脑壳疼了。

The drawback is that, it's obviously that filters conditions will continue to grow as well as duplication of the same filter for other query. In other hand, the maintainability of the code kind of headache.

来看看管道优雅的处理方式
There is where Pipeline become a hero

return User::query()->filter([     UsernameFilter::class,    EmailFilter::class,    AddressFilter::class,    OccupationFilter::class])->get();

简单而简短吧?看看下面的步骤

Simple and short right? But before that,

1. 创建一个名为"Filterable"的trait类并写一个scope方法

  1. Create a trait named Filterable and create a scope

class Filterable{        public function scopeFilter($query, array $through)       {                    return app(Pipeline::class)                   ->send($query)                               ->through($through)                               ->thenReturn();           }}

然后,你就可以愉快的在任意Model中复用它,如User模型

Then, use it in any model that you prefer, for example User model

class User {    use Filterable; }

2.创建一个Filter,例如UsernameFilter

2. Create a filter for example UsernameFilter

class UsernameFilter {    public function handle($query, $next)    {                if (request()->mobile_phone) {                      $query->where('username', request()->mobile_phone);              }                 return $next($query);      }}

食用方法:

The usage is just like this

User::query()->filter([UsernameFilter::class])->get();

或者

OR

你还可以通过传递属性的方式来使用管道。

If you want for more accessibility to the pipeline, you can also pass an attribute.

class StringFilter {    public function handle($query, $next, $column) {        if (request()->{$column}) {                       $query->where($column, 'LIKE', request()->{$column});              }         return $next($query);     }}

像下面这样用

The usage is just like this

User::query()->filter([   'StringFilter:username',   'StringFilter:email',])->get();

"Pipeline如何处理Laravel多条件查询"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

查询 条件 管道 代码 内容 可以通过 方式 方法 更多 模型 知识 面的 愉快 明显 简短 优雅 学有所成 接下来 不断 中间件 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 手冲咖啡和咖啡机区别 广州万易互联网科技有限公司 互联网通讯科技 互联网是不是新科技 重庆互联网络科技 腾讯阿里互联网科技 高科技与互联网 丰台科技园 互联网 上海万想互联网科技 上海亿帐通互联网科技 互联网科技公司名称 顺网科技2021年互联网排名 安控科技rtu产品工业互联网 昭通云南众福互联网科技有限公司 互联网科技与管理 青岛科技大学互联网大赛研究生 互联网媒体平台算高科技产业 工业互联网山东科技大学 山东大美朱楼互联网科技有限公司 江苏互联网智能科技产品出厂价 广东万易互联网科技有限公司惠州分公司 广州万易互联网科技有限公司 美国互联网保险保险科技 天下捷融互联网科技 科技赋能互联网健康 互联网实体化科技化 电子商务与互联网科技的关系 广州有谷有贝互联网科技有限公司 广西泛欧互联网科技有限公司 互联网企业和网络科技有限公司
0