[英]Laravel Eloquent: Nested Query Builder?
有什么办法可以代替 Eloquent 执行这个数据库查询吗?
DB::table(DB::raw('(select * from threads order by id limit 20) th'))->orderBy('votes')->paginate(10)
基本上我想通过投票来排序线程,但我不想为此请求表中的所有线程,而只是请求最近的 20 个线程。
谢谢你!
由于您想获取最新的 20 个线程,然后按投票对它们进行排序,因此可以使用sortByDesc()
方法:
$threads = Thread::latest()->take(20)->get();
$orderedThreads = $threads->sortByDesc('votes');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.