Count
The count method calculates how many records
match a given filter. It can return a simple integer or a structured result depending
on the provided options.
Purpose
The count method is used to count records from your model. It supports filters, detailed field-specific counts, and custom selection, making it useful for analytics or validations.
Parameters
-
$criteria— an associative array defining conditions, select options, or field-specific count configuration.
Return Value
Depending on the configuration, it returns:
- An integer (default)
- An associative array with selected fields
- An object, if the
formatoption is used
Error Handling
Throws an exception when:
- Invalid criteria format is provided
- A database error occurs during execution
Basic Example
$completedCount = $prisma->user->count();
echo "<pre>";
print_r($completedCount);
echo "</pre>";
Filtered Example
$completedCount = $prisma->user->count([
'where' => [
'name' => [
'contains' => 'John Doe',
],
]
]);
echo "<pre>";
print_r($completedCount);
echo "</pre>";
Example With Select
$userCount = $prisma->user->count([
'where' => [
'name' => ['contains' => 'John Doe'],
],
'select' => [
'id' => true,
'age' => true,
],
]);
echo "<pre>";
print_r($userCount);
echo "</pre>";