如何在Laravel 4中手动创建一个新的空的雄辩收藏 [英] How to manually create a new empty Eloquent Collection in Laravel 4

查看:89
本文介绍了如何在Laravel 4中手动创建一个新的空的雄辩收藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有一个 newCollection()方法可以被覆盖,这不是真的做工作,因为只有当我们查询一个设置的结果时才被使用。



我在想建立一个空的集合,然后填充Eloquent对象。我不是使用数组的原因是因为我喜欢Eloquent Collections方法,如包含



如果有其他的选择,我很乐意听到他们的消息。

解决方案

这不是很有口气,为您的收藏添加一个雄辩的模型有一些选择:



Laravel 5 中,您可以从帮助者中受益

  $ c = collect(new Post); 

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

OLD Laravel 4 ANSWER

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

然后你可以

  $ c-> add(new Post); 

或者您可以使用make:

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


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.

解决方案

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

这篇关于如何在Laravel 4中手动创建一个新的空的雄辩收藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆