首页 > 解决方案 > OFFSET 和 LIMIT 在 laravel Eloquent 中不起作用?

问题描述

我正在尝试对 Laravel eloquent 使用偏移量和限制。

$start_from = 0;
$limit = 2;
$invoices = Invoice::where('calculation_complete',0)
    ->offset($start_from)
    ->limit($limit)
    ->get();

它没有给我 2 条记录,而是给了我所有的记录。哪里出了问题,我在其他地方使用它,它工作正常。

标签: phplaraveleloquentlaravel-8

解决方案


Laravel 有自己的函数skipforoffsettakefor limit

尝试这个:

$start_from = 0;
$limit = 2;

$invoices = Invoice::where('calculation_complete',0)
                ->orderBy('id','DESC')
                ->skip($start_from)
                ->take($limit)
                ->get();

或者,您可以使用paginate

$invoices = Invoice::where('calculation_complete',0)
                ->orderBy('updated_at', 'desc')->paginate($limit);

推荐阅读