Is it possible to have an eloquent query builder return StdClass rather then Model?

例如,User::where('age', '>', 34)->get()返回User个型号中的Collection个.

DB::table('users')->where('age', '>', 34)->get()返回StdClass个对象的Collection.快多了.

因此:

Is it possible to prevent hydrating eloquent models and return StdClass objects as a database query builder would, but still leverage the usefulness of an eloquent query builder syntax?


我不明白为什么这个问题被否决了.它的期望和措辞相当清楚.人们应该将鼠标悬停在向下投票的图标上,并看到"这个问题没有显示任何研究成果;它不清楚,也没有帮助"的提示.

Laravel社区有一种强烈的信念,即如果你站在Eloquent 之外,你就是在过早地优化.这是不正确的,在有10000多行从数据库结果中提取有说服力的模型的情况下,速度非常慢.我们谈论的是几秒钟,有时是几十秒钟和沉重的记忆.

Laravelers would then state "Just do a DB::query() its faster". Yes.. Yes... it is im aware... Although I'm asking in this very question... right here... above.. clearly worded and concise... Can I use Eloquent Query Builder to build the query and have StdClass objects returned so as to not hydrate thousands of Eloquent models with all the overhead. Already I hear the Laravelers "Why not use DB::query() then?" Because I'm NOT asking that... Im asking can we use the Eloquent syntax its easier and more readable than doing joins....

我不是在这里询问有关应用程序架构的建议.我们希望这样做有很多深层次的原因,比如匹配ElasticSearch存储库的输出等等...但我不必进入"为什么"——这是一个问答论坛.不是讨论或建议平台.

  • Face palm

推荐答案

可以,可以使用‘getQuery’或‘toBase’方法.例如:

User::where('age', '>', 34)->getQuery()->get();

or

User::where('age', '>', 34)->toBase()->get();

Laravel相关问答推荐

运行NPM Prod时出现VUE问题

用Laravel Filament创建变形组件

验证判断请求的值是否存在于另一个表 Laravel 9 中

如何避免谷歌翻译翻译:参数

如何在自引用表上使用 Laravel 的 hasManyThrough

Laravel:使用 Input::old 时 textarea 不会重新填充

FileViewFinder.php 第 137 行中的 Laravel 5 InvalidArgumentException:未找到视图 [.admin]

Laravel 6 Passport 在错误的凭证上返回 400 Bad request

Laravel 5.4 存储:下载文件.文件不存在,但显然存在

Laravel 合集日期比较

将 Laravel MYSQL 更改为 utf8mb4 以在现有数据库中支持表情符号

laravel 预期响应代码 250 但得到代码530

如何从 Laravel 中的 hasMany() 关系中获取所有结果?

条带 api 判断现有卡

设置 Laravel 5.4 以使用 React

如何在 Laravel 5.2 中手动发送密码重置请求?

在 AWS 上找不到 Laravel 5 类Collective\Html\HtmlServiceProvider

Laravel php artisan 服务于模仿 HTTPS

Laravel - 使用 Eloquent 查询构建器在 Select 中添加自定义列

限制自己重载外部 API 的速率