如何使用 Laravel 的流式查询构建器进行计数选择?
Laravel 中的流式查询构建器是一个负责创建和运行数据库查询的接口。该查询构建器兼容 Laravel 支持的所有数据库,并可用于执行几乎所有数据库操作。
使用流式查询构建器的优势在于它可以防止 SQL 注入攻击。它利用 PDO 参数绑定,您可以根据需要自由发送字符串。
流式查询构建器 支持许多方法,例如 count、min、max、avg、sum,这些方法可以从表中获取聚合值。
现在让我们看看如何使用流式查询构建器在选择查询中获取计数。要使用 Fluent 查询生成器,请使用如下所示的 DB Facade 类
使用 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; } }
在 selectRaw() 方法中的 count() 函数中使用列 id,并使用 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; } }
在上面的例子中,我们想要在 students 表中查找姓名为 Rehan Khan 的学生人数。因此,查询语句如下:
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() 方法来检查表中是否存在任何记录,那么另一种方法是使用 exist() 或 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

