php 如何在 Laravel 4 中手动创建一个新的空 Eloquent Collection
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23599584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to manually create a new empty Eloquent Collection in Laravel 4
提问by JofryHS
How do we create a new Eloquent Collection in Laravel 4, without using Query Builder?
我们如何在 Laravel 4 中创建一个新的 Eloquent Collection,而不使用 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.
有一种newCollection()
方法可以被它覆盖,但它并不能真正起作用,因为它仅在我们查询一组结果时使用。
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
.
我想建立一个空的 Collection,然后用 Eloquent 对象填充它。我不使用数组的原因是因为我喜欢 Eloquent Collections 方法,例如contains
.
If there are other alternatives, I would love to hear them out.
如果有其他选择,我很想听听他们的意见。
回答by Antonio Carlos Ribeiro
It's not really Eloquent, to add an Eloquent model to your collection you have some options:
这不是真正的 Eloquent,要将 Eloquent 模型添加到您的集合中,您有一些选择:
In Laravel 5you can benefit from a helper
在Laravel 5 中,您可以从助手中受益
$c = collect(new Post);
or
或者
$c = collect();
$c->add(new Post);
OLD Laravel 4 ANSWER
旧 Laravel 4 答案
$c = new \Illuminate\Database\Eloquent\Collection;
And then you can
然后你可以
$c->add(new Post);
Or you could use make:
或者你可以使用 make:
$c = Collection::make(new Post);
回答by Gokigooooks
As of Laravel 5. I use the global function collect()
从 Laravel 5. 我使用全局函数 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
回答by Samuel Cloete
I've actually found that using newCollection()
is more future proof....
我实际上发现使用newCollection()
更能证明未来......
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
这样,如果您决定在稍后阶段为您的模型创建自己的集合类(就像我已经做过几次一样),那么重构代码会容易得多,因为您只需覆盖newCollection()
模型中的函数
回答by andrewtweber
Just to add on to the accepted answer, you can also create an alias in config/app.php
只是为了添加到已接受的答案,您还可以在 config/app.php
'aliases' => array(
...
'Collection' => Illuminate\Database\Eloquent\Collection::class,
Then you simply need to do
然后你只需要做
$c = new Collection;
回答by chebaby
Laravel >= 5.5
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:
如果你想手动创建一个新的空集合,你可以使用这样的collect
辅助方法:
$new_empty_collection = collect();
You can find this helper in Illuminate\Support\helpers.php
你可以在 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);
}
}
回答by Victor Aguilar
It is better to use the Injection Pattern and after $this->collection->make([])
than new Collection
这是更好地利用喷射图案和后$this->collection->make([])
比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([]);
...
}
回答by hdifen
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.
在 Laravel 5 和 Laravel 6 中,您可以将Illuminate\Database\Eloquent\Collection
类从服务容器中解析出来,然后将模型添加到其中。
$eloquentCollection = resolve(Illuminate\Database\Eloquent\Collection::class);
// or app(Illuminate\Support\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
有关了解从 laravel 中的服务容器解析对象的更多信息,请查看此处:https://laravel.com/docs/5.7/container#resolving