下面由laravel/" target="_blank">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();