订单清单未按order_date订购

时间:2019-03-12 09:27:44

标签: laravel laravel-5

我创建了一个页面,该页面列出了与登录用户匹配的数据库中的所有项目,我遇到的问题是我的列表不希望在订购日期前被订购。

这是我的代码

public function trackOrders()
{
    $menus_child = Menu::where('menu_id', 0)->with('menusP')->get();
    $contacts = Contact::all();

    $orders = Auth::user()->orders;
    $orders->transform(function($order, $key){
        $order->cart = unserialize($order->cart);
        return $order;
    })->sortByDesc('order_date');

    return view('public.users.track-orders', compact('menus_child', 'contacts', 'orders', 'order_item'));
}

我也尝试过删除此

->sortByDesc('order_date');

并添加

->orderBy('order_date', 'desc');

但是我得到了这个错误

  

方法Illuminate \ Database \ Eloquent \ Collection :: orderBy不存在。

2 个答案:

答案 0 :(得分:1)

尝试这样添加:

/// *** Creates an instance of a class from an assembly referenced through its disk path
IDispatch* WINAPI ClrCreateInstanceFrom(char *AssemblyFileName, char *className, char *ErrorMessage, DWORD *dwErrorSize)
{
    try {
        if (!spDefAppDomain)
            ClrLoad();
        CComPtr<_ObjectHandle> spObjectHandle;
        VerifyHresult(spDefAppDomain->CreateInstanceFrom(_bstr_t(AssemblyFileName), _bstr_t(className), &spObjectHandle));
        CComVariant VntUnwrapped;
        VerifyHresult(spObjectHandle->Unwrap(&VntUnwrapped));
        return VntUnwrapped.pdispVal;
    } catch (HresultException ex) {
        *dwErrorSize = ex.GetMessage(ErrorMessage);
        return NULL;
    }
}

答案 1 :(得分:0)

collections上的方法返回一个新的Collection实例并保留原始集合。因此,您需要将新的$orders分配给旧的变量,例如:

$orders = $orders->transform(function($order, $key){
    $order->cart = unserialize($order->cart);
    return $order;
})->sortByDesc('order_date');