如何使用Laravel的流暢查詢建構器選擇計數?
Laravel 中的流暢查詢建構器是負責建立和執行資料庫查詢的介面。查詢建構器可以與 Laravel 支援的所有資料庫配合良好,並且可以用來執行幾乎所有資料庫操作。
使用流暢的查詢產生器的優點是它可以防止 SQL 注入攻擊。它利用 PDO 參數綁定,您可以根據需要自由發送字串。
流暢的查詢建構器支援許多方法,例如count、min、max、avg、sum,可以從表格中取得總計值。
現在讓我們看看如何使用流暢的查詢建立器來取得選擇查詢中的計數。要使用流暢的查詢建立器,請使用資料庫外觀類,如下所示
use Illuminate\Support\Facades\DB;
現在讓我們檢查幾個範例以取得選擇查詢中的計數。假設我們使用以下查詢建立了一個名為 Students 的表格
CREATE TABLE students( id INTEGER NOT NULL PRIMARY KEY, name VARCHAR(15) NOT NULL, email VARCHAR(20) NOT NULL, created_at VARCHAR(27), updated_at VARCHAR(27), address VARCHAR(30) NOT NULL );
並填充它,如下所示 -
+----+---------------+------------------+-----------------------------+-----------------------------+---------+ | id | name | email | created_at | updated_at | address | +----+---------------+------------------+-----------------------------+-----------------------------+---------+ | 1 | Siya Khan | siya@gmail.com | 2022-05-01T13:45:55.000000Z | 2022-05-01T13:45:55.000000Z | Xyz | | 2 | Rehan Khan | rehan@gmail.com | 2022-05-01T13:49:50.000000Z | 2022-05-01T13:49:50.000000Z | Xyz | | 3 | Rehan Khan | rehan@gmail.com | NULL | NULL | testing | | 4 | Rehan | rehan@gmail.com | NULL | NULL | abcd | +----+---------------+------------------+-----------------------------+-----------------------------+---------+
表中記錄數為4。
範例 1
在下面的範例中,我們在 DB::table 中使用學生。 count() 方法負責傳回表中存在的總記錄。
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; class StudentController extends Controller{ public function index() { $count = DB::table('students')->count(); echo "The count of students table is :".$count; } }
輸出
上面範例的輸出是 -
The count of students table is :4
範例 2
在此範例中,將使用 selectRaw() 來取得表格中存在的記錄總數。
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; class StudentController extends Controller { public function index() { $count = DB::table('students')->selectRaw('count(id) as cnt')->pluck('cnt'); echo "The count of students table is :".$count; } }
列 ID 在 selectRaw() 方法的 count() 內部使用,並使用 pluck 來取得計數。
輸出
上述程式碼的輸出是 -
The count of students table is :[4]
範例 3
此範例將使用 selectRaw() 方法。假設您想要計算姓名數量,例如 Rehan Khan。讓我們看看如何將 selectRaw() 與 count() 方法一起使用
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; class StudentController extends Controller { public function index() { $count = DB::table('students')-> where('name', 'Rehan Khan')-> selectRaw('count(id) as cnt')->pluck('cnt'); echo "The count of name:Rehan Khan in students table is :".$count; } }
在上面的範例中,我們想要尋找表:學生中名為#Rehan Khan的人数b>.所以寫的查詢就是為了得到它。
DB::table('students')->where('name', 'Rehan Khan')->selectRaw('count(id) as cnt')->pluck('cnt');
我們使用了 selectRaw() 方法來計算 where 篩選器的記錄。最後使用pluck()方法取得計數值。
輸出
上述程式碼的輸出是 -
The count of name:Rehan Khan in students table is :[2]
範例 4
如果您打算使用count() 方法來檢查表中是否存在任何記錄,替代方法是您可以使用exists() 或doesntExist() 方法如下所示-
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; class StudentController extends Controller{ public function index() { if (DB::table('students')->where('name', 'Rehan Khan')->exists()) { echo "Record with name Rehan Khan Exists in the table :students"; } } }
輸出
上述程式碼的輸出是 -
Record with name Rehan Khan Exists in the table :students
範例 5
使用doesntExist()方法檢查給定表中是否有可用的記錄。
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; class StudentController extends Controller{ public function index() { if (DB::table('students')->where('name', 'Neha Khan')->doesntExist()) { echo "Record with name Rehan Khan Does not Exists in the table :students"; } else { echo "Record with name Rehan Khan Exists in the table :students"; } } }
輸出
上述程式碼的輸出是 -
Record with name Rehan Khan Does not Exists in the table :students
以上是如何使用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)

熱門話題

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是第三方函式庫,提供靈活語法、強大過濾器、擴充支援和安全沙箱。

Laravel - 分頁自訂 - Laravel 包含分頁功能,可協助使用者或開發人員包含分頁功能。 Laravel 分頁器與查詢產生器和 Eloquent ORM 整合。自動分頁方法
