Pipeline怎么处理Laravel多条件查询

广告:宝塔服务器面板,一键全能部署及管理,送你10850元礼包,点我领取~~~

Pipeline怎么处理Laravel多条件查询

下面由Laravel教程栏目给大家介绍Pipeline怎么处理Laravel多条件查询,希望对大家有所帮助!

原标题:Laravel Eloquent Query Filter using Pipeline原文链接:hafiqiqmal93.medium.com/laravel-eloquent-query-sfilter-using-pipeline-7c6f2673d5da

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方法

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();
登录后复制

搞定,优雅吧!

Done. Simple and clean

推荐:《最新的五个Laravel视频教程》

以上就是Pipeline怎么处理Laravel多条件查询的详细内容,更多请关注9543建站博客其它相关文章!

9543建站博客
一个专注于网站开发、微信开发的技术类纯净博客。
作者头像
admin创始人

肥猫,知名SEO博客站长,14年SEO经验。

上一篇:分析PHP7.2忽略父类方法以及Liskov替换原则相关问题
下一篇:技术分享:PHP图像操作函数

发表评论

关闭广告
关闭广告