Skip to content

Início / Optitravel / Models

find($id, array $columns = ['*'])

This method retrieves a single record from the database using the value of the model's primary key (usually id). It's one of the most commonly used methods when you need to fetch a specific entry by its unique identifier.

By default, it returns all the columns defined in $allowedColumns, but you can also pass a custom list of columns if you only need specific fields.

Parameters

  • $id – The value of the primary key to search for.

  • $columns (optional) – An array of specific column names to retrieve. If empty or ['*'], it returns all allowed columns.

Returns:

  • An object containing the result, mapped to the model's structure. Returns null if no record is found.

Example

Returns allowedColumns fields.

use \App\Models\User;

$user = new User();
$retreivedUser = $user->find(5);

echo $retreivedUser->email;

Custom column example:

Only returns id and email fields.

use \App\Models\User;

$user = new User();
$retreivedUser = $user->find(5, ['id', 'email']);
echo $retreivedUser->email;

(Última atualização: 06/05/2025)