To convert a Laravel Eloquent model to an array with keys representing the model’s attribute names and values representing the attribute values, you can use the toArray
method of the model. For example:
1 2 3 4 | $user = App\User::find(1); $userArray = $user->toArray(); |
The resulting $userArray
would be an array with keys representing the attribute names of the $user
model and values representing the attribute values.
If you want to convert a collection of Eloquent models to an array, you can use the toArray
method of the collection. For example:
1 2 3 4 | $users = App\User::all(); $usersArray = $users->toArray(); |
This would convert the collection of $users
to an array of arrays, with each inner array representing an individual model in the collection.