溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Pipeline如何處理Laravel多條件查詢

發(fā)布時間:2022-01-10 15:37:28 來源:億速云 閱讀:115 作者:iii 欄目:編程語言

本篇內(nèi)容介紹了“Pipeline如何處理Laravel多條件查詢”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

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.

基本上,通過管道,我們可以通過任務(wù)堆棧傳遞對象,并通過回調(diào)獲得結(jié)果。

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

管道用于查詢過濾的好處是我們可以將成噸的屎山減少到幾行。 沒用管道之前,我們通常會寫一個控制器,獲取用戶模型的 Eloquent 實(shí)例,并根據(jù)查詢字符串拼接一些條件。

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

缺點(diǎn)很明顯,過濾條件像屎山一樣不斷的堆加,出現(xiàn)大量重復(fù)的代碼。 另外,代碼的可維護(hù)性就有點(diǎn)腦殼疼了。

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.

來看看管道優(yōu)雅的處理方式
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. 創(chuàng)建一個名為“Filterable”的trait類并寫一個scope方法

  1. 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中復(fù)用它,如User模型

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

class User {
    use Filterable; }

2.創(chuàng)建一個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();

“Pipeline如何處理Laravel多條件查詢”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI