如何在不使用查询生成器的情况下,在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.

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

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

OLD Laravel 4 ANSWER

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

And then you can

$c->add(new Post);

或者你可以使用make:

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

Laravel相关问答推荐

如何在Laravel中从多行中获取合并数组?

工厂多对多数据透视表属性

Laravel 9 如何将 url 中的参数从一个控制器传递到另一个控制器?

Npm run dev 卡在 APP_URL

Laravel & Docker:无法打开流或文件/var/www/html/storage/logs/laravel.log:无法打开流:权限被拒绝

Laravel - 将变量从中间件传递到控制器/路由

如何在 Laravel 中使用内存数据库的完整测试套件之前迁移和 seeder ?

如何处理 Laravel 的 SMTP 驱动程序中的自签名 TLS 证书?

你如何找到 Laravel 外观背后的底层类?

在 laravel 查询构建器中使用多个 where 子句

如何更改 Laravel5 中的视图文件夹?

如何判断是否在laravel中设置了cookie?

Laravel:如何在没有数据库的情况下对用户进行身份验证

调用未定义的函数 mb_strimwidth

调用未定义的方法 Maatwebsite\Excel\Excel::load()

Laravel Eloquent 在子查询中有两个WHERE NOT IN

如何在 laravel 中解码 Json 对象并在 laravel 中应用 foreach 循环

laravel 5.1 在没有 VM 重启的情况下看不到作业(job)文件的更改

Laravel 验证存在于不存在的地方

错误:找不到模块 'webpack/lib/rules/DescriptionDataMatcherRulePlugin' 需要堆栈: