Introduction
Eloquent is the ORM (Object-Relational Mapper) included with Laravel, which is one of the most popular PHP frameworks. It provides an elegant and simplified syntax for interacting with your database. However, there are instances when you may want to convert your Eloquent query results into a PHP array for various reasons such as caching, manipulation, or simply because you need to meet specific data structure requirements.
In this tutorial, we’re going to discuss multiple ways to convert Eloquent query results to PHP arrays. We will start with simple conversions and work our way up to more complex scenarios. Examples will be provided along the way for clearer understanding.
Simple Conversion to Array
The most straightforward method to convert an Eloquent collection to an array is using the toArray()
method. After executing a query using Eloquent, you call this method on your results like so:
<?php
$users = App\User::all();
$usersArray = $users->toArray();
?>
This will give you a plain PHP array of the underlying data.
Converting with Relationships
When your Eloquent model has relationships, you might also want to include them in your array. Here’s how you do it:
<?php
$users = App\User::with('posts')->get();
$usersArray = $users->toArray();
?>
This includes the related ‘posts’ with each user in the resulting array.
Select Specific Columns
If you don’t need every column in the resulting array, you can select specific columns as follows:
<?php
$users = App\User::select('id', 'name', 'email')->get();
$usersArray = $users->toArray();
?>
This will create an array that only contains id
, name
, and email
for each user.
Converting a Single Model
If you are dealing with just one Eloquent model instance, you can convert it to an array using the same toArray()
method:
<?php
$user = App\User::find(1);
$userArray = $user->toArray();
?>
You now have an array with the user’s properties.
Handling Nested Relationships
To convert nested relationships, you’ll need to explicitly load them before calling toArray()
:
<?php
$user = App\User::with('posts.comments')->find(1);
$userArray = $user->toArray();
?>
This creates an array where each user has a ‘posts’ array, which in turn contains a ‘comments’ array.
Customizing the Array Format
Sometimes, we need to custom format our result arrays. Eloquent models can specify an toArray()
method to control the array format:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
public function toArray()
{
return [
'user_id' => $this->id,
'user_name' => $this->name,
'user_email' => $this->email,
// Other custom fields here
];
}
}
?>
Now, when you use toArray
, you’ll get your customized array structure.
Converting Loaded Relationships Separately
If you need to convert the loaded relationships separately, you can manually do so:
<?php
$user = App\User::with('posts')->find(1);
$userArray = $user->toArray();
$postArray = $user->posts->toArray();
?>
The $userArray
contains the user data, and $postArray
contains the related posts.
Using Collections
Remember that Eloquent queries return Illuminate\Support\Collection
instances for ‘get’ operations. Collections have their own toArray()
method:
<?php
$collection = collect(['name' => 'John Doe', 'email' => '[email protected]']);
$array = $collection->toArray();
?>
This demonstrates that collections and Eloquent results can be treated similarly when converting to arrays.
Conclusion
Converting Eloquent results to PHP arrays is a straightforward and powerful feature provided by Laravel. Whether you’re doing simple conversions or dealing with complex nested relationships, the toArray()
method alongside Eloquent’s relational capabilities and collections provide a clean and efficient way to work with PHP arrays.
With these tools and techniques, you’re now equipped to handle any situation where an Eloquent-to-PHP array conversion might be necessary in your Laravel applications.