php - OFFSET 和 LIMIT 在 laravel Eloquent 中不起作用?
问题描述
我正在尝试对 Laravel eloquent 使用偏移量和限制。
$start_from = 0;
$limit = 2;
$invoices = Invoice::where('calculation_complete',0)
->offset($start_from)
->limit($limit)
->get();
它没有给我 2 条记录,而是给了我所有的记录。哪里出了问题,我在其他地方使用它,它工作正常。
解决方案
Laravel 有自己的函数skip
foroffset
和take
for 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);
推荐阅读
- android - 在我的 KtorClient 的 DefaultRequest 中声明 ContentType = Application.Json 后,我可以更改特定请求的 ContentType 标头吗
- notion-api - 将@mention 对象添加到概念页面标题?
- html - 如何应用限制来控制具有可变内容的固定位置、居中 DIV 的尺寸?
- sql - Postgres 10.14 中的触发功能未按预期运行
- android - 显示标题并展开子项
- powerbi - Power BI 不同值的总和(按类别)
- plot - ImportError:无法从“绘图”导入名称“plot_learning_curve”
- python - 如何通过可点击的 django 制作组列表
- single-sign-on - 如何为 SSO 提供 AWS cognito 作为 SAML 2.0 IDP?
- c++ - 如何更改 Visual Studio 2019 C++ WinForms 中的标题栏颜色?