简体   繁体   English

Laravel - orderBy 不处理关系集合

[英]Laravel - orderBy not working on relationship collection

I recently moved a Laravel website to a different server with the same setup (slightly different versions of php, mariadb, etc.) and have run into an interesting problem.我最近将一个 Laravel 网站移动到具有相同设置的不同服务器(php、mariadb 等版本略有不同)并遇到了一个有趣的问题。

I'm using the package Team-Tea-Time/laravel-forum (formerly riari/laravel-forum) to provide message board functionalities.我正在使用 package Team-Tea-Time/laravel-forum (以前的 riari/laravel-forum)来提供留言板功能。 In the forum every thread has a link to get to the newest post, which worked fine before the move.在论坛中,每个线程都有一个指向最新帖子的链接,在移动之前它运行良好。 After the move those links lead to the oldest/first post instead.移动后,这些链接将指向最旧/第一个帖子。

The functionality is part of the Thread model , specifically the function getLastPostAttribute().该功能是线程 model的一部分,特别是 function getLastPostAttribute()。

When trying to get to the bottom of this, I realized that the orderBy on the posts relationship is simply not having any effect.当试图深入了解这一点时,我意识到 post 关系上的 orderBy 根本没有任何效果。 No matter what attribute I sort by (eg created_at, sequence) and which sort direction I request, it's always the same post order and the same first post being returned.无论我按什么属性排序(例如 created_at、sequence)和我请求的排序方向,它总是相同的发布顺序和相同的第一个帖子被返回。

Example of a thread with 1811 posts with different timestamps and sequence numbers:包含 1811 个不同时间戳和序列号的帖子的线程示例:

Ordering by sequence ASC works correctly, the first post being the one with the smalles sequence number.按序列 ASC 排序可以正常工作,第一个帖子是具有较小序列号的帖子。 This is however also the default order of that relationship (see Model).然而,这也是该关系的默认顺序(参见模型)。

$t->posts()->orderBy('sequence', 'asc')->first();
Riari\Forum\Models\Post {#3268
    id: 113544,
    thread_id: 3995,
    author_id: 191,
    sequence: 1,
    created_at: "2018-05-08 08:21:20",
    updated_at: "2018-05-08 10:51:41",
    deleted_at: null,
    vote_count: 0,
}

Now when I change the sort order to desc, it should return the post with the highest sequence number.现在,当我将排序顺序更改为 desc 时,它应该返回具有最高序列号的帖子。 But instead I get the same result as above.但相反,我得到了与上面相同的结果。

$t->posts()->orderBy('sequence', 'desc')->first();
Riari\Forum\Models\Post {#3297
    id: 113544,
    thread_id: 3995,
    author_id: 191,
    sequence: 1,
    created_at: "2018-05-08 08:21:20",
    updated_at: "2018-05-08 10:51:41",
    deleted_at: null,
    vote_count: 0,
}

This is the post that should be returned when sorting by sequence desc:这是按序列desc排序时应该返回的帖子:

Riari\Forum\Models\Post {#3332
    id: 196449,
    thread_id: 3995,
    author_id: 534,
    sequence: 1814,
    created_at: "2020-12-16 14:55:54",
    updated_at: "2020-12-16 14:55:54",
    deleted_at: null,
    vote_count: 0,
}

Has anybody experienced this before?以前有人经历过吗? Everything else works fine after the move, I'm clueless as to what would cause such a specific error.搬家后其他一切正常,我不知道是什么导致了这种特定的错误。

When you look at the relation definition in the source code of Thread.php , you see that posts are already ordered by created_at by default:当您查看Thread.php源代码中的关系定义时,您会看到默认情况下posts已按created_at排序:

public function posts()
{
        $withTrashed = config('forum.preferences.display_trashed_posts') || Gate::allows('viewTrashedPosts');
        $query = $this->hasMany(Post::class)->orderBy('created_at');
        return $withTrashed ? $query->withTrashed() : $query;
}

I think you would need to override the posts method, remove the orderBy('created_at') statement, and then your own orderBy will work.我认为您需要覆盖posts方法,删除orderBy('created_at')语句,然后您自己的 orderBy 将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM