如何在Laravel 4中手动创建一个新的空的雄辩收藏 [英] How to manually create a new empty Eloquent Collection in 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屋!