如何使用Laravel的流畅查询构建器选择计数?

来自:互联网
时间:2023-09-04
阅读:

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的流畅查询构建器选择计数?的详细内容。

返回顶部
顶部