To get data as an array from an Eloquent model, you can use the toArray()
method.
Here’s an example:
1 2 3 4 | $users = User::all(); $usersArray = $users->toArray(); |
This will retrieve all the users from the database and convert them into an array.
Alternatively, you can also use the toJson()
method to get the data as a JSON string.
1 2 3 4 | $users = User::all(); $usersJson = $users->toJson(); |
You can then decode the JSON string into an array using json_decode()
, like this:
1 2 3 | $usersArray = json_decode($usersJson, true); |