下面由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多条件查询的详细内容。更多信息请关注PHP中文网其他相关文章!

Laravel适合快速构建Web应用,而Python适用于更广泛的应用场景。 1.Laravel提供EloquentORM、Blade模板引擎和Artisan工具,简化Web开发。 2.Python以动态类型、丰富的标准库和第三方生态系统着称,适用于Web开发、数据科学等领域。

Laravel和Python各有优势:Laravel适合快速构建功能丰富的Web应用,Python在数据科学和通用编程领域表现出色。1.Laravel提供EloquentORM和Blade模板引擎,适合构建现代Web应用。2.Python拥有丰富的标准库和第三方库,Django和Flask框架满足不同开发需求。

Laravel值得选择,因为它能使代码结构清晰,开发过程更具艺术性。1)Laravel基于PHP,遵循MVC架构,简化Web开发。2)其核心功能如EloquentORM、Artisan工具和Blade模板增强了开发的优雅与健壮性。3)通过路由、控制器、模型和视图,开发者能高效构建应用。4)队列和事件监听等高级功能进一步提升应用性能。

Laravel不仅是后端框架,还是完整的Web开发解决方案。它提供了强大的后端功能,如路由、数据库操作、用户认证等,并支持前端开发,提升了整个Web应用的开发效率。

Laravel适合Web开发,Python适用于数据科学和快速原型开发。 1.Laravel基于PHP,提供优雅的语法和丰富功能,如EloquentORM。 2.Python以简洁着称,广泛应用于Web开发和数据科学,拥有丰富的库生态系统。

laravelcanbeeffectefection ininreal-worldapplications forbuildingscalablewebsolutions.1)ITSImplifieCrudoperationsInrestfulaPisusingEloquentorm.2)laravel'secosystem,包括Toolslikenova,包括Toolslikenova,增强功能

Laravel在后端开发中的核心功能包括路由系统、EloquentORM、迁移功能、缓存系统和队列系统。1.路由系统简化了URL映射,提高了代码组织和维护性。2.EloquentORM提供了面向对象的数据操作,提升了开发效率。3.迁移功能通过版本控制管理数据库结构,确保一致性。4.缓存系统减少数据库查询,提升响应速度。5.队列系统有效处理大规模数据,避免阻塞用户请求,提升整体性能。

Laravel在后端开发中表现强大,通过EloquentORM简化数据库操作,控制器和服务类处理业务逻辑,并提供队列、事件等功能。1)EloquentORM通过模型映射数据库表,简化查询。2)业务逻辑在控制器和服务类中处理,提高模块化和可维护性。3)其他功能如队列系统帮助处理复杂需求。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

Atom编辑器mac版下载
最流行的的开源编辑器

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

禅工作室 13.0.1
功能强大的PHP集成开发环境

WebStorm Mac版
好用的JavaScript开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)