Laravel - Axois请求中不存在方法[orderBy]

时间:2018-03-18 07:28:54

标签: php laravel laravel-5 vue.js laravel-eloquent

我正试图从我的数据库中获取一些记录,但我得到Method [orderBy] does not exist我正在向Axois提出此请求。

路线

Route::get('/get-order-statuses', 'Backend\Orders\OrderStatuses@index');

控制器代码

public function index()
    {
        $statuses = OrderStatuses::orderBy('created_at', 'desc')
            ->get(['id', 'status', 'status_description']);

        return response()
            ->json([
                'statuses' => $statuses
            ]);
    }

型号代码

protected $table = 'order_statuses';
    protected $fillable = [
        'statues', 'status_description', 'userId',
    ];

Vue模板

import { get } from '../../helpers/api'
        export default {
            data() {
                return {
                    isLoading: true,
                    loaded: false,
                    statuses: []
                }
            },
            created() {
                get('get-order-statuses')
                    .then((res) => {
                        console.log(res);
                        this.isLoading = false;
                        this.loaded = true;
                    })
            }

1 个答案:

答案 0 :(得分:3)

orderByQueryBuilder的一种方法,因此在使用它之前,您应该获得QueryBuilder的实例。

同样get不会将字段作为其参数。

考虑到上述情况,您的查询可以重写为:

$statuses = OrderStatuses::select('id', 'status', 'status_description')
    ->orderBy('created_at', 'desc')
    ->get();

或者:

$statuses = DB::table('order_statuses')
    ->select('id', 'status', 'status_description')
    ->orderBy('created_at', 'desc')
    ->get();

另外

$statuses = OrderStatuses::all()
    ->select('id', 'status', 'status_description')
    ->orderBy('created_at', 'desc')
    ->get();