首頁 > php框架 > Laravel > Pipeline怎麼處理Laravel多條件查詢

Pipeline怎麼處理Laravel多條件查詢

藏色散人
發布: 2022-01-11 09:57:35
轉載
1949 人瀏覽過

下方由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.


  1. ##基本上,透過管道,我們可以透過任務堆疊傳遞對象,並透過回調獲得結果。
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 get instance of EUserlodelent , 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#.

#來看看管道優雅的處理方式

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中文網其他相關文章!

相關標籤:
來源:learnku.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板