渴望将关系分类为不起作用的Laravel

发布于 2025-01-22 23:22:14 字数 1493 浏览 0 评论 0原文

我试图使用急切的加载来通过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')没有ddtosql,我得到的输出就像忽略整个急切的负载一样。

我是想念什么还是做错了什么?在这种情况下,我的关系看起来像这样:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空心↖ 2025-01-29 23:22:14

在阅读全文@timlewis所指的全文后,我使用了Join解决方案。

SomeModel::query()->select('some_table.id', 'some_table.name') # Only bring back what we need without the join
    ->join('some_other_table', 'some_other_table.id', '=', 'some_table.some_other_table_id')
    ->orderByDesc('column');

另外,您总是可以leftjoin,如果您不必始终期望数据,只需进行一些零异常处理即可。

我实际上创建了一个抽象类,该类具有一个全局范围,可以在基于哈希映射方法的任何模型中动态执行此操作。

After reading through the full article as @TimLewis alluded to, I went with the join solution.

SomeModel::query()->select('some_table.id', 'some_table.name') # Only bring back what we need without the join
    ->join('some_other_table', 'some_other_table.id', '=', 'some_table.some_other_table_id')
    ->orderByDesc('column');

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文