[英]How to manually create a new empty Eloquent Collection in Laravel 4
我们如何在 Laravel 4 中创建一个新的 Eloquent 集合,而不使用查询生成器?
有一个newCollection()
方法可以被它覆盖,但它并没有真正起作用,因为它只在我们查询一组结果时使用。
我正在考虑构建一个空的 Collection,然后用 Eloquent 个对象填充它。 我不使用数组的原因是因为我喜欢 Eloquent Collections 方法,例如contains
。
如果还有其他选择,我很乐意听取他们的意见。
这不是真正的 Eloquent,要将 Eloquent 模型添加到您的集合中,您有一些选择:
在Laravel 5 中,您可以从助手中受益
$c = collect(new Post);
或者
$c = collect();
$c->add(new Post);
旧 Laravel 4 答案
$c = new \Illuminate\Database\Eloquent\Collection;
然后你可以
$c->add(new Post);
或者你可以使用 make:
$c = Collection::make(new Post);
从 Laravel 5. 我使用全局函数collect()
$collection = collect([]); // initialize an empty array [] inside to start empty collection
这种语法非常干净,如果您不想要数字索引,也可以添加偏移量,如下所示:
$collection->offsetSet('foo', $foo_data); // similar to add function but with
$collection->offsetSet('bar', $bar_data); // an assigned index
我实际上发现使用newCollection()
更具前瞻性......
例子:
$collection = (new Post)->newCollection();
这样,如果您决定在稍后阶段为您的模型创建自己的集合类(就像我已经做过几次一样),那么重构代码会容易得多,因为您只需覆盖模型中的newCollection()
函数
只是为了添加到已接受的答案,您还可以在config/app.php
创建别名
'aliases' => array(
...
'Collection' => Illuminate\Database\Eloquent\Collection::class,
然后你只需要做
$c = new Collection;
这可能与原始问题无关,但由于它是 google 搜索中的第一个链接之一,我发现这对像我这样正在寻找如何创建空集合的人很有帮助。
如果你想手动创建一个新的空集合,你可以像这样使用collect
辅助方法:
$new_empty_collection = collect();
你可以在Illuminate\\Support\\helpers.php
找到这个助手
片段:
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);
}
}
最好使用注入模式并且在$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([]);
...
}
在 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());
有关了解从 laravel 中的服务容器解析对象的更多信息,请查看此处: https ://laravel.com/docs/5.7/container#resolving
我正在使用这种方式:
$coll = new Collection();
$coll->name = 'name';
$coll->value = 'value';
$coll->description = 'description';
并将其用作普通收藏
dd($coll->name);
对我有用的是命名 use 命名空间并直接实例化它:
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
# Usage
$this->latest_posts = new EloquentCollection();
允许我合并 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.