Início / Optitravel / Models
findBy(string $column, $value, array $columns = ['*'])
This method retrieves records from the database based on a specific column and its value. It's useful when you need to search for records based on criteria other than the primary key (e.g., searching for users by email or products by category).
By default, it returns all the columns defined in $allowedColumns, but you can also specify which columns you want to retrieve by passing an array of column names.
Parameters
-
$column – The name of the column to search by (e.g., email, username).
-
$value – The value of the column to match (e.g., 'john.doe@example.com').
-
$columns (optional) – An array of specific column names to retrieve. If empty or ['*'], it returns all allowed columns.
Returns:
- An array of objects, each representing a record that matches the search criteria.
Example
Returns allowedColumns fields.
use \App\Models\User;
$user = new User();
$users = $user->findBy('status', 'active');
foreach ($users as $user) {
echo $user->name;
}
Custom column example:
Only returns id and email fields.
use \App\Models\User;
$user = new User();
$users = $user->findBy('status', 'active', ['id', 'email']);
foreach ($users as $user) {
echo $user->email;
}
↑ (Última atualização: 06/05/2025)