Laravel 12 E-Commerce Project Tutorial
Implementing a Database-Backed Shopping Cart
Hi everyone, welcome back to the Laravel E-Commerce Tutorial. This video focuses on transitioning the shopping cart functionality from session-based storage to database storage. This allows the shopping cart items to persist for authenticated users across sessions, meaning a user'scart is restored when they log back in.
-
Publish the Shoppingcart Migration
The first step is to use the dedicated command to publish the database migration file for the shopping cart package (assuming the "surfsidemediaShoppingcart" package is used):
php artisan vendor:publish --provider="SurfsidemediaShoppingcartShoppingcartServiceProvider" --tag="migrations" -
Run the Migration
Execute the migration to create the necessary table (`shoppingcart`) in your database for storing cart instances linked to user IDs:
php artisan migrate -
Create Events and Listeners
Create an event and a listener to handle the saving of the cart to the database and the restoring of the cart from the database upon login.
php artisan make:event CartUpdated
php artisan make:listener StoreCartListener --event=CartUpdated -
Implement the `Login` Function Logic
In your `LoginController` (or wherever your login logic resides), after a successful user login, implement the logic to restore the cart instance specific to the authenticated user'sID. This replaces the default login redirection with a custom one that performs the cart logic.
// Assuming this code is inside the authenticated method in LoginController or a custom login handler
// ... successful login logic ...
// Restore the cart instance for the authenticated user
Cart::instance("cart")->restore($request->user()->id);
// Cart::instance("wishlist")->restore($request->user()->id); // (Included for completeness based on common e-commerce pattern)
// Check if user has 2FA enabled (part of the overall application logic)
$user = $request->user();
if ($user->google2fa_enabled) {
// Don'tlog in yet, redirect to 2FA verification
return redirect()->route("2fa.verify.show")->with("status", "Please verify with your 2FA code");
}
// Redirect the user to the intended or default page
return redirect()->intended(route("home.index")); -
Implement the `Logout` Function Logic
Similarly, in your `LogoutController` or custom logout handler, before logging the user out, ensure the current state of the cart is saved (stored) to the database against the user'sID.
// Assuming this code is inside the custom logout method
// Save the current cart instance to the database before logging out
Cart::instance("cart")->store(Auth::user()->id);
// Cart::instance("wishlist")->store(Auth::user()->id); // (Included for completeness)
// Proceed with default logout logic
Auth::guard("web")->logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
return redirect("/");
Verification and Testing 🛒💾
1. Log in as User A and add several products to the cart.
2. Log out (the cart items should be saved to the database).
3. Log in as User B (cart should be empty or contain User B'ssaved items).
4. Log out.
5. Log in again as User A. The cart should now display the original products added in step 1, confirming the database storage and retrieval functionality is working correctly.
