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

0 votes
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.

How do we create a new Eloquent Collection in Laravel, without using Query Builder?
Aug 11 in Laravel by kartik
• 27,140 points
198 views

1 answer to this question.

0 votes

Hello @kartik,

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);

Hope it helps!!
Thank You!

answered Aug 11 by Niroj
• 58,500 points

Related Questions In Laravel

0 votes
1 answer

How to update a pivot table using Eloquent in laravel 5?

Hello @kartik, The code below solved my problem: $messages ...READ MORE

answered 1 day ago in Laravel by Niroj
• 58,500 points
10 views
0 votes
1 answer

How to make a new page with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18 in Laravel by Niroj
• 58,500 points
144 views
0 votes
1 answer

How to mock a static facade methods in Laravel?

Hey, Facades provide a "static" interface to classes ...READ MORE

answered Mar 19 in Laravel by Niroj
• 58,500 points
495 views
0 votes
1 answer

What is database migration and how to create database migration in Laravel?

Hii, Migrations are like version control for your database, ...READ MORE

answered Mar 23 in Laravel by Niroj
• 58,500 points
151 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18 in Laravel by Niroj
• 58,500 points
2,168 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18 in Laravel by Niroj
• 58,500 points
236 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23 in Laravel by Niroj
• 58,500 points
189 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23 in Laravel by Niroj
• 58,500 points
2,682 views
0 votes
1 answer

How can we create a record in Laravel using eloquent?

We need to create a new model ...READ MORE

answered Mar 20 in Laravel by Niroj
• 58,500 points
95 views
+1 vote
1 answer

How to add a new column to existing table of laravel in a migration?

Hii @kartik, To create a migration, you may ...READ MORE

answered Mar 30 in Laravel by Niroj
• 58,500 points
10,093 views