【问题标题】:Laravel: Call to undefined method orderByLaravel:调用未定义的方法 orderBy
【发布时间】:2015-05-31 02:52:05
【问题描述】:

我想在 Laravel 中使用“orderBy”按降序对列表进行排序。

我收到一条错误消息:调用未定义的方法 orderBy。

这是我的控制器

 $sections = Section1::all()->orderBy('name', 'DESC')->get();

【问题讨论】:

    标签: laravel laravel-4 eloquent declaration laravel-5


    【解决方案1】:

    当您运行Section1::all() 时,您会以对象的形式获得结果,因此您无法链接查询构建器,例如orderBy。您正在寻找的是:

     $sections = Section1::orderBy('name', 'DESC')->get();
    

    这表示为 Sections1 模型构建一个按名称降序排序的查询并获取结果。

    您不能同时使用all()get()

    【讨论】:

    • ooooohhhh 我明白了:D 感谢您的提示:3 并且代码有效!谢谢!!! :3
    猜你喜欢
    • 1970-01-01
    • 2013-10-23
    • 1970-01-01
    • 2016-06-05
    • 2016-09-03
    • 2015-06-10
    • 2016-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多