首頁  >  文章  >  後端開發  >  如何使用Laravel的流暢查詢建構器選擇計數?

如何使用Laravel的流暢查詢建構器選擇計數?

WBOY
WBOY轉載
2023-09-04 22:17:06714瀏覽

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

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除