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

选择Laravel开发项目是因为其灵活性和强大功能适应不同规模和复杂度的需求。Laravel提供路由系统、EloquentORM、Artisan命令行等功能,支持从简单博客到复杂企业级系统的开发。

Laravel和Python在开发环境和生态系统上的对比如下:1.Laravel的开发环境简单,仅需PHP和Composer,提供了丰富的扩展包如LaravelForge,但扩展包维护可能不及时。2.Python的开发环境也简单,仅需Python和pip,生态系统庞大,涵盖多个领域,但版本和依赖管理可能复杂。

Laravel是如何在后端逻辑中发挥作用的?它通过路由系统、EloquentORM、认证与授权、事件与监听器以及性能优化来简化和增强后端开发。1.路由系统允许定义URL结构和请求处理逻辑。2.EloquentORM简化数据库交互。3.认证与授权系统便于用户管理。4.事件与监听器实现松耦合代码结构。5.性能优化通过缓存和队列提高应用效率。

Laravel受欢迎的原因包括其简化开发过程、提供愉快的开发环境和丰富的功能。1)它吸收了RubyonRails的设计理念,结合PHP的灵活性。2)提供了如EloquentORM、Blade模板引擎等工具,提高开发效率。3)其MVC架构和依赖注入机制使代码更加模块化和可测试。4)提供了强大的调试工具和性能优化方法,如缓存系统和最佳实践。

Django和Laravel都是全栈框架,Django适合Python开发者和复杂业务逻辑,Laravel适合PHP开发者和优雅语法。1.Django基于Python,遵循“电池齐全”哲学,适合快速开发和高并发。2.Laravel基于PHP,强调开发者体验,适合小型到中型项目。

PHP和Laravel不是直接可比的,因为Laravel是基于PHP的框架。1.PHP适合小型项目或快速原型开发,因其简单直接。2.Laravel适合大型项目或高效开发,因其提供丰富功能和工具,但学习曲线较陡,性能可能不如纯PHP。

laravelisabackendframeworkbuiltonphp,设计ForweBapplicationDevelopment.itfocusessonserver-sideLogic,databasemagemention和Applicationstructure和CanBeintegratedWithFrontendTechnologiesLikeLikeVue.jsorreActeReacterVue.jsorreActforforfull-stackDevefloct。


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

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

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

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