Início / Optitravel / Models
countWhere(array $conditions = [])
This method returns the total number of records from the model's table that match a given set of conditions. It’s useful when you need to count how many records meet specific criteria, such as active users, items in stock, or filtered results for pagination.
You can use operators like =, !=, <, >, LIKE, IN, and BETWEEN in your condition keys.
Parameters
- array $conditions – An associative array of conditions. The key can include the column and optional operator (e.g., 'status' => 'A', 'price >' => 100, 'name LIKE' => '%test%'). Only columns declared in allowedColumns are permitted.
Returns:
- An integer representing the number of rows that match the conditions.
Example
use \App\Models\User;
$user = new User();
$count = $user->countWhere([
'status' => 'A',
'email LIKE' => '%@optigest.net'
]);
echo "Active Optigest users: $count";
Notes
-
If no conditions are provided, it will return the total number of rows in the table.
-
All condition columns must be in the model’s allowedColumns list, or an exception will be thrown.
↑ (Última atualização: 06/05/2025)