首页 文章

Laravel Eloquent:在相关表格中按字段排序结果

提问于
浏览
0

如何通过相关表中的字段来排序查询结果?

我有两张 table ,

表用户:id,first_name,last_name等

表格视频:id,user_id,title等

型号:视频

public function user()
{
    return $this->belongsTo('App\Models\User');
}

例如,我正在查询title =“video 1”的所有视频,并使用skip&take for pagination / pager .

我现在想用用户名来订购这个视频列表 .

$query = \App\Models\Video::where('title','=','Video 1')->skip(0)->take(10);

这样做的最佳/有效方法是什么?我无法在查询后对数组/集合进行排序,因为分页将不起作用 .

谢谢 .

1 回答

  • 0

    你为什么不用:

    $query = \App\Models\Video::where('title','=','Video 1')
    ->join("users","users.id","=","user_id")
    ->orderBy("users.first_name")->skip(0)->take(10);
    

相关问题