Examples of displaying cart data in Laravel Blade templates.
{{-- resources/views/cart.blade.php --}}
[@if](https://github.com/if)(Cart::isEmpty())
<p>Your cart is empty.</p>
[@else](https://github.com/else)
<div class="cart">
<h2>Shopping Cart</h2>
<div class="cart-items">
[@foreach](https://github.com/foreach)(Cart::items() as $item)
<div class="cart-item">
<h4>{{ $item->name }}</h4>
<p>Price: {{ $item->unitPrice()->formatted() }}</p>
<p>Quantity: {{ $item->quantity }}</p>
<p>Subtotal: {{ $item->subtotal()->formatted() }}</p>
[@if](https://github.com/if)($item->attributes)
<div class="attributes">
[@foreach](https://github.com/foreach)($item->attributes as $key => $value)
<span class="attribute">{{ $key }}: {{ $value }}</span>
[@endforeach](https://github.com/endforeach)
</div>
[@endif](https://github.com/endif)
<form action="{{ route('cart.remove') }}" method="POST">
[@csrf](https://github.com/csrf)
<input type="hidden" name="item_id" value="{{ $item->id }}">
<button type="submit">Remove</button>
</form>
</div>
[@endforeach](https://github.com/endforeach)
</div>
<div class="cart-totals">
<p>Subtotal: {{ Cart::subtotal()->formatted() }}</p>
<p><strong>Total: {{ Cart::total()->formatted() }}</strong></p>
</div>
<div class="cart-actions">
<a href="{{ route('cart.clear') }}" class="btn btn-secondary">Clear Cart</a>
<a href="{{ route('checkout') }}" class="btn btn-primary">Checkout</a>
</div>
</div>
[@endif](https://github.com/endif)
{{-- resources/views/components/cart-summary.blade.php --}}
<div class="cart-summary">
<span class="cart-count">{{ Cart::count() }} items</span>
<span class="cart-total">{{ Cart::total()->formatted() }}</span>
</div>
{{-- resources/views/cart-with-discounts.blade.php --}}
<div class="cart-totals">
<div class="subtotal">
<span>Subtotal:</span>
<span>{{ Cart::subtotal()->formatted() }}</span>
</div>
[@foreach](https://github.com/foreach)(Cart::conditions() as $condition)
<div class="condition {{ $condition->value < 0 ? 'discount' : 'fee' }}">
<span>{{ $condition->name }}:</span>
<span>{{ $condition->formattedValue() }}</span>
</div>
[@endforeach](https://github.com/endforeach)
<div class="total">
<span><strong>Total:</strong></span>
<span><strong>{{ Cart::total()->formatted() }}</strong></span>
</div>
</div>
[@foreach](https://github.com/foreach)(Cart::items() as $item)
<div class="cart-item">
<h4>{{ $item->name }}</h4>
<p>Unit Price: {{ $item->unitPrice()->formatted() }}</p>
<p>Quantity: {{ $item->quantity }}</p>
[@if](https://github.com/if)($item->conditions()->isNotEmpty())
<div class="item-conditions">
[@foreach](https://github.com/foreach)($item->conditions() as $condition)
<span class="badge">{{ $condition->name }}: {{ $condition->formattedValue() }}</span>
[@endforeach](https://github.com/endforeach)
</div>
<p>Adjusted Total: {{ $item->total()->formatted() }}</p>
[@else](https://github.com/else)
<p>Item Total: {{ $item->subtotal()->formatted() }}</p>
[@endif](https://github.com/endif)
</div>
[@endforeach](https://github.com/endforeach)
<form action="{{ route('cart.update') }}" method="POST">
[@csrf](https://github.com/csrf)
[@method](https://github.com/method)('PATCH')
<input type="hidden" name="item_id" value="{{ $item->id }}">
<input type="number"
name="quantity"
value="{{ $item->quantity }}"
min="1"
max="99"
class="quantity-input">
<button type="submit">Update</button>
</form>
<div class="cart-totals">
<div class="subtotal">
<span>Subtotal:</span>
<span>{{ Cart::subtotal()->formatted() }}</span>
</div>
<div class="taxable-subtotal">
<span>Taxable Amount:</span>
<span>{{ Cart::getTaxableSubtotal()->formatted() }}</span>
</div>
{{-- Display tax conditions --}}
[@foreach](https://github.com/foreach)(Cart::conditions()->filter(fn($c) => $c->taxable) as $tax)
<div class="tax-line">
<span>{{ $tax->name }}:</span>
<span>{{ $tax->formattedValue() }}</span>
</div>
[@endforeach](https://github.com/endforeach)
<div class="total">
<span><strong>Total:</strong></span>
<span><strong>{{ Cart::total()->formatted() }}</strong></span>
</div>
</div>
{{-- resources/views/components/mini-cart.blade.php --}}
<div class="mini-cart" x-data="{ open: false }">
<button [@click](https://github.com/click)="open = !open" class="cart-toggle">
<svg>...</svg>
[@if](https://github.com/if)(Cart::count() > 0)
<span class="badge">{{ Cart::uniqueCount() }}</span>
[@endif](https://github.com/endif)
</button>
<div x-show="open" class="mini-cart-dropdown">
[@if](https://github.com/if)(Cart::isEmpty())
<p>Your cart is empty</p>
[@else](https://github.com/else)
[@foreach](https://github.com/foreach)(Cart::items()->take(3) as $item)
<div class="mini-cart-item">
<span>{{ $item->name }}</span>
<span>{{ $item->quantity }} x {{ $item->unitPrice()->formatted() }}</span>
</div>
[@endforeach](https://github.com/endforeach)
[@if](https://github.com/if)(Cart::uniqueCount() > 3)
<p class="more-items">+ {{ Cart::uniqueCount() - 3 }} more items</p>
[@endif](https://github.com/endif)
<div class="mini-cart-total">
<strong>Total: {{ Cart::total()->formatted() }}</strong>
</div>
<a href="{{ route('cart.index') }}" class="view-cart-btn">View Cart</a>
[@endif](https://github.com/endif)
</div>
</div>
[@if](https://github.com/if)(Cart::isEmpty())
<div class="empty-cart">
<svg class="empty-cart-icon">...</svg>
<h3>Your cart is empty</h3>
<p>Looks like you haven't added anything to your cart yet.</p>
<a href="{{ route('products.index') }}" class="btn btn-primary">
Continue Shopping
</a>
</div>
[@endif](https://github.com/endif)
// routes/web.php
Route::prefix('cart')->name('cart.')->group(function () {
Route::get('/', [CartController::class, 'index'])->name('index');
Route::post('/add', [CartController::class, 'add'])->name('add');
Route::patch('/update', [CartController::class, 'update'])->name('update');
Route::delete('/remove', [CartController::class, 'remove'])->name('remove');
Route::post('/clear', [CartController::class, 'clear'])->name('clear');
});
<?php
namespace App\Http\Controllers;
use Daikazu\Flexicart\Facades\Cart;
use Illuminate\Http\Request;
class CartController extends Controller
{
public function index()
{
return view('cart.index');
}
public function add(Request $request)
{
$validated = $request->validate([
'id' => 'required',
'name' => 'required|string',
'price' => 'required|numeric|min:0',
'quantity' => 'integer|min:1',
'attributes' => 'array',
]);
Cart::addItem($validated);
return back()->with('success', 'Item added to cart!');
}
public function update(Request $request)
{
$validated = $request->validate([
'item_id' => 'required',
'quantity' => 'required|integer|min:1',
]);
Cart::updateItem($validated['item_id'], [
'quantity' => $validated['quantity'],
]);
return back()->with('success', 'Cart updated!');
}
public function remove(Request $request)
{
Cart::removeItem($request->input('item_id'));
return back()->with('success', 'Item removed from cart!');
}
public function clear()
{
Cart::clear();
return back()->with('success', 'Cart cleared!');
}
}
How can I help you explore Laravel packages today?