0
votes

I created a page that lists all the items in a database that matches the logged in user, the problem I'm having is that my list doesn't want to be ordered by the order date.

This is my code

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'));
}

I've also tried removing this

->sortByDesc('order_date');

and adding this

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

but then I got this error

Method Illuminate\Database\Eloquent\Collection::orderBy does not exist.

2

2 Answers

1
votes

try to add like this:

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

The methods on collections return a new Collection instance and preserve the original collection. Therefore you need to assign the new $orders to the old variable, like:

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