Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Pipeline deal with Laravel multi-conditional queries

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how Pipeline handles Laravel multi-conditional queries". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Pipeline is one of the most useful features of Laravel. Pipeline is also one of the most commonly used components in Laravel, such as middleware.

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, through the pipeline, we can pass objects through the task stack and get the results through callbacks.

Basically, with a pipeline we can pass an object through a stack of tasks and get the result via a callback.

The advantage of pipelines for query filtering is that we can reduce tons of shit mountains to a few rows. Before we don't use pipelines, we usually write a controller, get an Eloquent instance of the user model, and concatenate some conditions according to the query string.

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 take a look at the shit Mountain query Dafa below.

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 disadvantage is obvious: the filter conditions are constantly piled up like a mountain of shit, resulting in a large number of repetitive code. In addition, the maintainability of the code has a bit of a brain pain.

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.

Let's take a look at the elegant handling of the pipeline.

There is where Pipeline become a hero

Return User::query ()-> filter ([UsernameFilter::class, EmailFilter::class, AddressFilter::class, OccupationFilter::class)-> get ()

Simple and short, right? Look at the following steps

Simple and short right? But before that

1. Create a trait class named "Filterable" and write a scope method

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 ();}}

Then you can happily reuse it in any Model, such as the User model

Then, use it in any model that you prefer, for example User model

Class User {use Filterable;}

two。 Create a Filter, such as 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);}}

How to eat:

The usage is just like this

User::query ()-> filter ([UsernameFilter::class])-> get ()

Or

OR

You can also use pipes by passing properties.

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);}}

Use it like this

The usage is just like this

User::query ()-> filter (['StringFilter:username',' StringFilter:email',])-> get (); that's all for "how Pipeline handles Laravel multi-conditional queries". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report