Pipeline怎麼處理Laravel多條件查詢
下方由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 thepipeline. 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 get instance of EUserlodelent , and apply some condition based on query string.
讓我們看看下面的屎山查詢大法。
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#.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();登入後複製
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 namedFilterable
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); }}
User::query()->filter([UsernameFilter::class])->get();
class StringFilter { public function handle($query, $next, $column) { if (request()->{$column}) { $query->where($column, 'LIKE', request()->{$column}); } return $next($query); }}
User::query()->filter([ 'StringFilter:username', 'StringFilter:email',])->get();
以上是Pipeline怎麼處理Laravel多條件查詢的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

透過管道進行檔案讀寫:建立一個管道從檔案讀取資料並透過管道傳遞從管道中接收資料並處理將處理後的資料寫入檔案使用goroutine並發執行這些操作以提高效能

Laravel9和CodeIgniter4的最新版本提供了更新的功能和改進。 Laravel9採用MVC架構,提供資料庫遷移、驗證及模板引擎等功能。 CodeIgniter4採用HMVC架構,提供路由、ORM和快取。在性能方面,Laravel9的基於服務提供者設計模式和CodeIgniter4的輕量級框架使其具有出色的性能。在實際應用中,Laravel9適用於需要靈活性和強大功能的複雜項目,而CodeIgniter4適用於快速開發和小型應用程式。

比較Laravel和CodeIgniter的資料處理能力:ORM:Laravel使用EloquentORM,提供類別物件關係映射,而CodeIgniter使用ActiveRecord,將資料庫模型表示為PHP類別的子類別。查詢建構器:Laravel具有靈活的鍊式查詢API,而CodeIgniter的查詢建構器更簡單,基於陣列。資料驗證:Laravel提供了一個Validator類,支援自訂驗證規則,而CodeIgniter的驗證功能內建較少,需要手動編碼自訂規則。實戰案例:用戶註冊範例展示了Lar

對於初學者來說,CodeIgniter的學習曲線更平緩,功能較少,但涵蓋了基本需求。 Laravel提供了更廣泛的功能集,但學習曲線稍陡。在性能方面,Laravel和CodeIgniter都表現出色。 Laravel有更廣泛的文件和活躍的社群支持,而CodeIgniter更簡單、輕量級,具有強大的安全功能。在建立部落格應用程式的實戰案例中,Laravel的EloquentORM簡化了資料操作,而CodeIgniter需要更多的手動配置。

在選擇大型專案框架時,Laravel和CodeIgniter各有優勢。 Laravel針對企業級應用程式而設計,提供模組化設計、相依性注入和強大的功能集。 CodeIgniter是一款輕量級框架,更適合小型到中型項目,強調速度和易用性。對於具有複雜需求和大量用戶的大型項目,Laravel的強大功能和可擴展性更為合適。而對於簡單專案或資源有限的情況下,CodeIgniter的輕量級和快速開發能力則較為理想。

Laravel - Artisan 指令 - Laravel 5.7 提供了處理和測試新指令的新方法。它包括測試 artisan 命令的新功能,下面提到了演示?

對於小型項目,Laravel適用於大型項目,需要強大的功能和安全性。 CodeIgniter適用於非常小的項目,需要輕量級和易用性。

比較了Laravel的Blade和CodeIgniter的Twig模板引擎,根據專案需求和個人偏好進行選擇:Blade基於MVC語法,鼓勵良好程式碼組織和模板繼承。 Twig是第三方函式庫,提供靈活語法、強大過濾器、擴充支援和安全沙箱。
