信息发布→ 登录 注册 退出

Pipeline怎么处理Laravel多条件查询

发布时间:2022-01-10

点击量:

下面由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();
标签:
在线客服
服务热线

服务热线

13331157271

微信咨询
二维码
返回顶部
×二维码

截屏,微信识别二维码

微信号:

(点击微信号复制,添加好友)

打开微信

微信号已复制,请打开微信添加咨询详情!