bumbummen99 / LaravelShoppingcart

A simple shopping cart implementation for Laravel
MIT License
505 stars 234 forks source link

Get a collection of Cart items in Descending order #131

Closed manishlok closed 3 years ago

manishlok commented 3 years ago

Hi there, I am using $cart = Cart::content( ); to make a collection. How can I show items in Descending order?

rrpadilla commented 3 years ago

If you use this fork, check this PR You should be able to do this:

Controller:


namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Product;
use Gloudemans\Shoppingcart\Cart;
use Illuminate\Http\Request;

class CartController extends Controller
{
    protected Cart $cart;

    public function __construct(Cart $cart)
    {
        $this->cart = $cart;
    }

    public function index(Request $request)
    {
        $cartContent = $this->cart->contentWithRelations(function ($ids) {
            return Product::with('image')
                ->whereIn('id', $ids)
                ->orderBy('price') // USE ORDER HERE.
                ->get();
        });

        return view('cart.index', [
            'cart' => $this->cart,
            'cartContent' => $cartContent,
        ]);
    }
}

I'll submit a new PR to the original repo covering more tests .

bumbummen99 commented 3 years ago

content does return a Collection of CartItem's so you could use reverse or sortBy.

github-actions[bot] commented 3 years ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days