How to manually create a new empty Eloquent Collection in Laravel 4
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Title: How to Manually Create a New Empty Eloquent Collection in Laravel 4
Introduction: In Laravel 4, when it comes to working with collections of models, Eloquent Collections can prove to be quite useful. Though Eloquent Collections are automatically created using the Query Builder, we might at times need to initialize an empty one for custom use cases. This blog post aims to guide you through creating a new, empty Eloquent Collection and discuss alternative approaches to handling collections in Laravel 4.
- Initialize Empty Eloquent Collection
If you're comfortable with PHP and want complete control over your collection, you can create an empty Eloquent Collection by using the custom collection class. First, define a new collection class extending the base Eloquent Collection class:
<?php
class EmptyCollection extends \Illuminate\Database\Eloquent\Collection {
/**
* The storage for all models in this collection.
*
* @var array
*/
protected $items = [];
}Now that you've defined the custom EmptyCollection class, you can instantiate a new instance:
<?php
$myEmptyCollection = new \App\CustomCollections\EmptyCollection();- Fill the Collection with Eloquent Objects
To fill your newly created collection with Eloquent objects, you'll need to loop through each model object and add them to your empty collection:
<?php
// Loop through all models
foreach ($models as $model) {
// Add the Eloquent object to the collection
$myEmptyCollection->push($model);
}Alternative Approaches for Handling Collections in Laravel 4
If you prefer working with arrays instead of collections, you can use native PHP functions or Laravel-specific methods. Here are a few examples:Use the ArrayUtils helper provided by Laravel to merge multiple collections/arrays into one:
<?php
use Illuminate\Support\Collection;
use Illuminate\Support\Arr;
// Create two collections
$collection1 = new Collection([0 => 'John', 1 => 'Larry']);
$collection2 = new Collection([0 => 'Paul', 1 => 'Mary']);
// Merge the collections into an array
$mergedCollection = Arr::merge($collection1->all(), $collection2->all());- Utilize Laravel's support for Eloquent relationships to create nested collections:
<?php
use Illuminate\Database\Eloquent\Collection as BaseCollection;
class Post extends Eloquent {
// Define relationship with Comment model
public function comments() {
return $this->hasMany(Comment::class);
}
}
// Fetch all posts and their comments
$posts = App\Post::with('comments')->get();
// Access the nested collection containing comments of each post
foreach ($posts as $post) {
// Iterate over comment collection using $post->comments
foreach ($post->comments as $comment) {
echo $comment->content;
}
}- Conclusion
Creating a new, empty Eloquent Collection in Laravel 4 can be achieved in various ways depending on your requirements and project needs. Whether you prefer using the custom collection class or alternative approaches such as arrays and nested relationships, this blog post has provided insights for different use cases to help you effectively manage collections within your application. For more information on Eloquent Collections and Laravel querying, please refer to our comprehensive tutorials at https://laravelcompany.com/blog/.