Skip to content

Início / Optitravel / Models

toArray()

This method converts the model's allowed columns into an associative array, where the keys are the column names and the values are the corresponding property values of the object.

It makes it easy to work with the model's data in an array format, for example, when preparing data for APIs or responses.

Returns:

  • An associative array with the column names as keys and the column values as values.

Example

use \App\Models\User;

$user = new User();
$user->id = 1;
$user->name = 'Teste Optigest';
$user->email = 'example@optigest.net';

$userArray = $user->toArray();

print_r($userArray);

Output:

Array
(
    ['id']    => 1
    ['name']  => 'Teste Optigest'
    ['email'] => 'example@optigest.net'
)

Notes

  • The method only includes columns defined in the allowedColumns property. This ensures that no unwanted or sensitive data is exposed.

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