How to manually create a new empty Eloquent Collection in Laravel 4

PhpLaravelLaravel 4Eloquent

Php Problem Overview


How do we create a new Eloquent Collection in Laravel 4, without using Query Builder?

There is a newCollection() method which can be overridden by that doesn't really do job because that is only being used when we are querying a set result.

I was thinking of building an empty Collection, then fill it with Eloquent objects. The reason I'm not using array is because I like Eloquent Collections methods such as contains.

If there are other alternatives, I would love to hear them out.

Php Solutions


Solution 1 - Php

It's not really Eloquent, to add an Eloquent model to your collection you have some options:

In Laravel 5 you can benefit from a helper

$c = collect(new Post);

or

$c = collect();
$c->add(new Post);

OLD Laravel 4 ANSWER

$c = new \Illuminate\Database\Eloquent\Collection;

And then you can

$c->add(new Post);

Or you could use make:

$c = Collection::make(new Post);

Solution 2 - Php

As of Laravel 5. I use the global function collect()

$collection = collect([]); // initialize an empty array [] inside to start empty collection

this syntax is very clean and you can also add offsets if you don't want the numeric index, like so:

$collection->offsetSet('foo', $foo_data); // similar to add function but with
$collection->offsetSet('bar', $bar_data); // an assigned index

Solution 3 - Php

I've actually found that using newCollection() is more future proof....

Example:

$collection = (new Post)->newCollection();

That way, if you decide to create your own collection class for your model (like I have done several times) at a later stage, it's much easier to refactor your code, as you just override the newCollection() function in your model

Solution 4 - Php

Laravel >= 5.5

>This may not be related to the original question, but since it's one of the first link in google search, i find this helpful for those like me, who are looking for how to create empty collection.

If you want to manually create a new empty collection, you can use the collect helper method like this:

$new_empty_collection = collect();

You can find this helper in Illuminate\Support\helpers.php

snippet:

if (! function_exists('collect')) {
    /**
     * Create a collection from the given value.
     *
     * @param  mixed  $value
     * @return \Illuminate\Support\Collection
     */
    function collect($value = null)
    {
        return new Collection($value);
    }
}

Solution 5 - Php

Just to add on to the accepted answer, you can also create an alias in config/app.php

'aliases' => array(

    ...
    'Collection'      => Illuminate\Database\Eloquent\Collection::class,

Then you simply need to do

$c = new Collection;

Solution 6 - Php

In Laravel 5 and Laravel 6 you can resolve the Illuminate\Database\Eloquent\Collection class out of the service container and then add models into it.

$eloquentCollection = resolve(Illuminate\Database\Eloquent\Collection::class);
// or app(Illuminate\Database\Eloquent\Collection::class). Whatever you prefer, app() and resolve() do the same thing.

$eloquentCollection->push(User::first());

For more information about understanding resolving objects out of the service container in laravel take a look here: https://laravel.com/docs/5.7/container#resolving

Solution 7 - Php

I am using this way :

$coll = new Collection();
    
$coll->name = 'name';
$coll->value = 'value';
$coll->description = 'description';

and using it as normal Collection

dd($coll->name);

Solution 8 - Php

It is better to use the Injection Pattern and after $this->collection->make([]) than new Collection

use Illuminate\Support\Collection;
...
// Inside of a clase.
...
public function __construct(Collection $collection){
    $this->collection = $collection;
}

public function getResults(){
...
$results = $this->collection->make([]);
...
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJofryHSView Question on Stackoverflow
Solution 1 - PhpAntonio Carlos RibeiroView Answer on Stackoverflow
Solution 2 - PhpGokigooooksView Answer on Stackoverflow
Solution 3 - PhpSamuel CloeteView Answer on Stackoverflow
Solution 4 - PhpchebabyView Answer on Stackoverflow
Solution 5 - PhpandrewtweberView Answer on Stackoverflow
Solution 6 - PhpTom HeadifenView Answer on Stackoverflow
Solution 7 - PhpHyzyrView Answer on Stackoverflow
Solution 8 - PhpVictor AguilarView Answer on Stackoverflow