mysql - Laravel Eloquent: order results by field in related table -
how order results of query field in related table?
i have 2 tables,
table users: id, first_name, last_name, etc
table videos: id, user_id, title, etc
model: video
public function user() { return $this->belongsto('app\models\user'); }
for example querying videos title = "video 1" , using skip & take pagination/pager.
i want order list of videos users first name.
$query = \app\models\video::where('title','=','video 1')->skip(0)->take(10);
what best/efficient way of doing this? can't order array/collection after query pagination won't work.
thanks.
why don't use:
$query = \app\models\video::where('title','=','video 1') ->join("users","users.id","=","user_id") ->orderby("users.first_name")->skip(0)->take(10);
Comments
Post a Comment