来自Laravel 4 and Eloquent ORM - How to select the last 5 rows of a table ,但我的问题有点不同。
如何返回按照创建方式 (ASC) 排序的最后 N 条记录。
例如,以下记录按顺序插入:
first
second
third
fourth
fifth
我想要一个查询返回最后 2 条记录
fourth
fifth
最佳答案
Laravel 偏移
DB::table('users')->skip(<NUMBER Calulation>)->take(5)->get();
您可以通过获取当前查询的计数并跳过 $query->count() - 5 来获取最后 5 条记录或您想要的任何内容来计算 N。
例如
$query = User::all();
$count = ($query->count()) - 5;
$query = $query->skip($count)->get();
关于laravel - 如何按输入顺序返回最后 n 条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28265690/