首页 > 解决方案 > 如何在 Laravel 6 LazyCollection 中使用 orderBy?

问题描述

Laravel 发布其最新版本 6 已经有几天了。我正在尝试实现 Laravel LazyCollection 而不是普通的 Collection 类。

以下线路工作 -

Drug::where('deactive',0)->orderBy('code')->get();

当我使用

Drug::cursor()->where('deactive',0)->orderBy('code')->get();

我收到一个错误Method Illuminate\Support\LazyCollection::orderBy does not exist.

谁能帮助我如何将 OrderBy 与 LazyCollection 一起使用?

更新

在这里得到答案后是上述查询的正确语法

Drug::cursor()->where('deactive',0)->sortBy('code');

标签: laravellaravel-6

解决方案


当然,orderBy方法是查询生成器方法。您可以按照文档sortBy中的定义使用


推荐阅读