[英]How to manually create a new empty Eloquent Collection in Laravel 4
How do we create a new Eloquent Collection in Laravel 4, without using Query Builder?我们如何在 Laravel 4 中创建一个新的 Eloquent 集合,而不使用查询生成器?
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.我正在考虑构建一个空的 Collection,然后用 Eloquent 个对象填充它。 The reason I'm not using array is because I like Eloquent Collections methods such as
contains
.我不使用数组的原因是因为我喜欢 Eloquent Collections 方法,例如
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:这不是真正的 Eloquent,要将 Eloquent 模型添加到您的集合中,您有一些选择:
In Laravel 5 you 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);
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
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()
函数
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;
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.
这可能与原始问题无关,但由于它是 google 搜索中的第一个链接之一,我发现这对像我这样正在寻找如何创建空集合的人很有帮助。
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);
}
}
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([]);
...
}
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\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有关了解从 laravel 中的服务容器解析对象的更多信息,请查看此处: https ://laravel.com/docs/5.7/container#resolving
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); dd($coll->name);
What worked for me was to name the use namespace and instantiate it directly:对我有用的是命名 use 命名空间并直接实例化它:
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
# Usage
$this->latest_posts = new EloquentCollection();
Allowed me to merge two data subsets of eloquent collection results, this maintains the relationships - a regular collection ( collect()
) loses relationship and probably some more metadata.允许我合并 eloquent 收集结果的两个数据子集,这维护了关系 - 常规收集(
collect()
)失去了关系,可能还有一些元数据。
$limit = 5;
$this->latest_posts = new EloquentCollection();
$pinned_posts = PinnedPostReference::where('category', $category)->get();
if($pinned_posts->count() > 0) {
foreach($pinned_posts as $ppost) {
$this->latest_posts->push($ppost->post);
}
}
# Another Eloquent result set ($regular_posts)
foreach($regular_posts as $regular_post) {
$this->latest_posts->push($regular_post);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.