Php 按不同表列列出的LAVEL订单表

Php 按不同表列列出的LAVEL订单表,php,sql,laravel,select,eloquent,Php,Sql,Laravel,Select,Eloquent,因此,我有表线程和表注释,我想根据插入到Comments.created_at中的最后一条注释对线程进行排序,我如何做到这一点 $threads = Thread::where('subboaId', $id) ->orderBy('comments.created_at', 'desc') ->get(); 尝试了此功能,但不起作用,可能我必须加入他们或其他什么使用whereHas函数(): 首先,必须在线程模型中添加关系: p

因此,我有表线程和表注释,我想根据插入到Comments.created_at中的最后一条注释对线程进行排序,我如何做到这一点

    $threads = Thread::where('subboaId', $id)
           ->orderBy('comments.created_at', 'desc')
           ->get();
尝试了此功能,但不起作用,可能我必须加入他们或其他什么

使用whereHas函数():

首先,必须在线程模型中添加关系:

public function comments()
{
    return $this->hasMany( Comment::class );
}
然后您可以在控制器上使用它,如下所示:

$threads = Thread::where('subboaId', $id)->whereHas( 'comments', function( $query ){
    $query->orderBy( 'created_at', 'desc' );
} )->get();
希望有帮助。

使用whereHas函数():

首先,必须在线程模型中添加关系:

public function comments()
{
    return $this->hasMany( Comment::class );
}
然后您可以在控制器上使用它,如下所示:

$threads = Thread::where('subboaId', $id)->whereHas( 'comments', function( $query ){
    $query->orderBy( 'created_at', 'desc' );
} )->get();

希望能有所帮助。

加入他们,试试这个

    $threads = Thread::join('comments', 'comments.id', '=', 'threads.comment_id')
                ->where('threads.subboaId', $id)
                ->orderBy('comments.created_at', 'desc')
                ->get();    

我希望这将帮助/引导你

加入他们,试试这个

    $threads = Thread::join('comments', 'comments.id', '=', 'threads.comment_id')
                ->where('threads.subboaId', $id)
                ->orderBy('comments.created_at', 'desc')
                ->get();    

我希望这将帮助/引导你

您使用的是什么版本的Laravel?4.x或5.x?您使用的是什么版本的Laravel?4.x或5.x?使用此选项,仅显示带有注释的线程,而且它是由从线程创建的线程排序的,它忽略注释:SYes它是唯一带有注释的显示线程,因为您根据注释排序行,您希望对没有注释的线程做什么@反社会者如果线程没有评论,我想比较他创建的和创建的,看看谁应该留下来top@Sociopath,更好的方法是在有新注释时始终更新线程的updated_at,因此您只需按线程的updated_at排序,这样只会显示带有注释的线程,另外,它是由线程创建的,它忽略注释:SYes,它只显示带有注释的线程,因为您根据注释对行进行排序,您希望对没有注释的线程做什么@反社会者如果线程没有评论,我想比较他创建的和创建的,看看谁应该留下来top@Sociopath,更好的方法是在有新注释时始终更新线程的更新位置,因此您只需按线程的更新位置排序即可