Overview
The order system manages the complete order lifecycle from cart to order confirmation. It supports both authenticated and guest users, with cart persistence for logged-in users.Data Models
Cart Model
The Cart model stores temporary items before order confirmation:app/Models/Cart.php
While the Cart model is minimal, it includes implicit fields:
user_id, food_id, quantity, and timestamps through database schema.Order Model
The Order model represents confirmed customer orders:app/Models/Order.php
Name of the food item ordered
Number of items ordered
Price per item at time of order
Customer name for delivery
Customer contact number
Delivery address
Cart Controller
The CartController manages cart operations for authenticated users.View Cart
app/Http/Controllers/CartController.php
Add to Cart
app/Http/Controllers/CartController.php
Duplicate Prevention
Duplicate Prevention
The
updateOrCreate() method prevents duplicate cart entries. If the user already has this food item in their cart, it updates the quantity instead of creating a new record.Remove from Cart
app/Http/Controllers/CartController.php
Security Check: The destroy method verifies that the authenticated user owns the cart item before allowing deletion.
Order Confirmation Flow
TheorderConfirm method in HomeController processes cart items into confirmed orders.
Order Confirmation Method
app/Http/Controllers/HomeController.php
Legacy Format Support
Transaction Processing
Transaction Safety Explained
Transaction Safety Explained
Why Transactions?
- Creates order and order items atomically
- Updates order total after all items are processed
- Clears cart items only if order succeeds
- Rolls back everything if any step fails
- Prevents partial orders or orphaned cart items
Input Formats
The orderConfirm method accepts two payload formats:- Structured Format (Recommended)
- Legacy Format
Cart Routes
All cart and order confirmation routes require authentication:routes/web.php
Order Lifecycle
1. Browse Menu
1. Browse Menu
2. Add to Cart
2. Add to Cart
- User must be authenticated
- POST to
/cart/{food}with quantity - Creates or updates cart item
- Cart persists across sessions
3. Review Cart
3. Review Cart
- View cart at
/cart - See all items with quantities and prices
- Can remove items or adjust quantities
4. Confirm Order
4. Confirm Order
- Submit delivery information
- POST to
/orderconfirmwith items and customer data - Creates order in database transaction
- Clears cart on success
5. Admin Processing
5. Admin Processing
- Admins view orders at
/admin/orders - Track order status (pending, processing, completed)
- View customer details and order items
Admin Order Management
Admins have dedicated routes for order oversight:routes/web.php
- ✅ Admin: Full access to all orders
- ❌ Chef: No access
- ❌ Mesero: No access
- ❌ Customers: Can only view their own orders
Best Practices
- Use Transactions: Always wrap order creation and cart clearing in transactions
- Validate Ownership: Check user_id before allowing cart modifications
- Prevent Duplicates: Use
updateOrCreate()for cart items - Price Snapshot: Store prices in orders (don’t reference Food prices)
- Guest Support: Handle both authenticated and guest users
- Error Handling: Use try-catch with transaction rollback
- Eager Loading: Load cart items with food data to prevent N+1
Common Queries
Consider implementing order status tracking (pending → preparing → ready → delivered) for better customer experience and kitchen workflow management.