渴望将关系分类为不起作用的Laravel
我试图使用急切的加载来通过Laravel中的关系动态顺序。
dd(SomeModel::with(['someRelation' => function ($query) {
$query->orderByDesc('column');
})->toSql());
我正在使用dd()
和tosql()
尝试调试发生的事情,这就是我看到的:
“从“ some_table”中select * where“ some_table”。“ deleted_at” is null”
,无论i order> orderby('columnby','asc','asc')
或order> order> order> desc')
没有dd
或tosql
,我得到的输出就像忽略整个急切的负载一样。
我是想念什么还是做错了什么?在这种情况下,我的关系看起来像这样:
class SomeModel
{
protected $table = 'some_table'; # for visual aid
public function someRelation(): BelongsTo
{
$this->belongsTo(SomeOtherModel::class)->select('id', 'column');
}
}
仅供参考,以后再调试。我试图尝试查看该函数是否执行,它执行的操作:
SomeModel::with(['someRelation' => function ($query) {
$query->orderByDesc('column');
dd($query->toSql());
});
dd
block执行告诉我执行的功能并给我:
“选择“ id”,“ name”来自“ some_other_table”,其中“ some_other_table”。“ in(?,??)和“ some_other_table”中的“ id”。“ deleted_at”是按“ name desc”
。
更新以检查Subqueries SQL:
\DB::enableQueryLog();
SomeModel::with(['someRelation' => function ($test) {
$test->orderBy('name', 'DESC');
}])->get();
dd(\DB::getQueryLog());
这使我返回一个空数组:
[]
I am trying to use Eager Loading to dynamically order by relationships in Laravel.
dd(SomeModel::with(['someRelation' => function ($query) {
$query->orderByDesc('column');
})->toSql());
I'm using dd()
and toSql()
to try to debug what is happening and this is what I see:
"select * from "some_table" where "some_table"."deleted_at" is null"
No matter if I orderBy('column', 'ASC')
or orderBy('column', 'DESC')
without the dd
or toSql
, I get the same output as if it is ignoring the entire eager load.
Am I missing something or doing something wrong? My relation in this case looks like this:
class SomeModel
{
protected $table = 'some_table'; # for visual aid
public function someRelation(): BelongsTo
{
$this->belongsTo(SomeOtherModel::class)->select('id', 'column');
}
}
FYI, some more debug later. I attempted to try see if the function ever executes, to which it does:
SomeModel::with(['someRelation' => function ($query) {
$query->orderByDesc('column');
dd($query->toSql());
});
The dd
block executes telling me the function executed and gives me:
"select "id", "name" from "some_other_table" where "some_other_table"."id" in (?, ?) and "some_other_table"."deleted_at" is null order by "name" desc"
Any help appreciated.
Update to check for subqueries SQL:
\DB::enableQueryLog();
SomeModel::with(['someRelation' => function ($test) {
$test->orderBy('name', 'DESC');
}])->get();
dd(\DB::getQueryLog());
This returns me an empty array:
[]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在阅读全文@timlewis所指的全文后,我使用了
Join
解决方案。另外,您总是可以
leftjoin
,如果您不必始终期望数据,只需进行一些零异常处理即可。我实际上创建了一个抽象类,该类具有一个全局范围,可以在基于哈希映射方法的任何模型中动态执行此操作。
After reading through the full article as @TimLewis alluded to, I went with the
join
solution.Alternatively, you can always
leftJoin
if you don't expect data all the time, just make some null exception handling.I actually created an abstract class which has a global scope to do this dynamically in any Model based on a hash-map approach.