Add Element at First Index of Collection
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Add Element at First Index of an Eloquent Collection: A Practical Guide
As developers working with Laravel and Eloquent, we frequently deal with collections of models. Understanding how to manipulate these collections—especially inserting elements at specific positions—is crucial for building dynamic and efficient applications.
The scenario you've described—wanting to prepend a new item to an Eloquent Collection but struggling with standard PHP array functions like array_unshift—is a very common hurdle. It highlights the difference between manipulating raw PHP arrays and working with Laravel’s specialized Illuminate\Database\Eloquent\Collection objects.
This post will walk you through why your initial attempt failed and provide the most robust, developer-friendly methods to achieve the desired outcome.
Why array_unshift Fails on Eloquent Collections
You correctly identified that standard array functions are often the first thought when manipulating lists. However, as demonstrated by the error:
array_unshift(): Argument #1 ($array) must be of type array, Illuminate\Database\Eloquent\Collection given
The issue is clear: an Eloquent Collection is an object of type Illuminate\Database\Eloquent\Collection, not a simple indexed PHP array. While Laravel collections are designed to behave like arrays in many contexts (they implement the Traversable interface), they do not automatically inherit all the methods available on native PHP arrays, which is why functions expecting a pure array throw type errors.
To successfully manipulate the order of elements, we must explicitly convert the Collection into a standard PHP array first, perform the modification, and then, if necessary, reconstruct the Collection.
The Solution: Converting to Array for Prepending
The most reliable way to prepend an element is to extract the underlying data into a mutable structure (an array), insert the new element at index 0, and then re-establish the relationship with the Eloquent Collection.
Here is the step-by-step approach using your FamilyMember example:
Step 1: Retrieve the Collection Data
First, fetch your collection as you normally would:
$family_members = FamilyMember::all();
Step 2: Convert to a Mutable Array
Use the toArray() method on the collection to get the underlying data structure. This allows us to use standard PHP array functions.
$members_array = $family_members->toArray();
Step 3: Prepare the New Element and Prepend
Create the new item you wish to add, and then use the native array_unshift function on this mutable array.
$new_member = new FamilyMember();
$new_member->id = "all_family_member";
$new_member->name = "All Family Member";
// Prepend the new member to the start of the array
array_unshift($members_array, $new_member);
Step 4: Rebuild the Eloquent Collection
Finally, convert the modified array back into an Eloquent Collection. This ensures that your object maintains its type integrity and adheres to Laravel’s conventions.
$updated_family_members = new FamilyMemberCollection($members_array);
// Or, in modern PHP/Laravel:
$updated_family_members = new Collection($members_array);
Complete Code Example
Here is the consolidated code demonstrating the safest way to prepend an element:
use Illuminate\Database\Eloquent\Collection;
use App\Models\FamilyMember; // Assuming your model namespace
// 1. Fetch the initial collection
$family_members = FamilyMember::all();
// 2. Prepare the new element
$tmp_all_members = new FamilyMember();
$tmp_all_members->id = "all_family_member";
$tmp_all_members->name = "All Family Member";
// 3. Convert to array, prepend, and rebuild
$members_array = $family_members->toArray();
array_unshift($members_array, $tmp_all_members);
// Recreate the collection from the updated array
$updated_family_members = new Collection($members_array);
// Now $updated_family_members has the new element at index 0.
Conclusion
Manipulating Eloquent Collections requires understanding their underlying structure. When you encounter issues with standard PHP functions like array_unshift, remember that the key is to explicitly bridge the gap between the Collection object and native arrays using methods like toArray(). This pattern ensures your code remains robust, predictable, and adheres to best practices within the Laravel ecosystem. For deeper insights into Eloquent relationships and collection handling, always refer back to the official documentation at laravelcompany.com.