Laravel

How to convert a Laravel Eloquent model to an array with keys1 min read

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:

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:

This would convert the collection of $users to an array of arrays, with each inner array representing an individual model in the collection.

Leave a Comment