Laravel 12 E-Commerce Project Tutorial

Implementing a Database-Backed Wishlist

Hi everyone, welcome back to the Laravel E-Commerce Tutorial. Following the cart implementation, this video focuses on making the Wishlist persistent by saving and restoring it from the database for authenticated users. This ensures the wishlist items remain even after the user logs out and logs back in.

  1. Create Events and Listeners for Wishlist

    Create a dedicated event and listener to handle the saving and restoring of the wishlist instance, similar to the cart setup, but using the wishlist instance name.

    php artisan make:event WishlistUpdated
    php artisan make:listener StoreWishlistListener --event=WishlistUpdated
  2. Define `WishlistUpdated` Event

    Open the `WishlistUpdated.php` file and define a public property to pass the user'semail (or ID) when the event is dispatched.

    class WishlistUpdated
    {
    use Dispatchable, InteractsWithSockets, SerializesModels;
    public $userEmail;
    public function __construct(string $userEmail)
    {
    $this->userEmail = $userEmail;
    }
    }

  3. Implement `Login` Function Logic (Wishlist Restore)

    In your `LoginController` (or custom login handler), update the authenticated method to restore the wishlist instance associated with the authenticated user'sID/Email after a successful login. (Note: The source uses `Auth::user()->email` for the key.)

    // ... inside the custom login authenticated method ...
    // Restore the cart instance (from previous part)
    Cart::instance("cart")->restore(Auth::user()->email);
    // Restore the wishlist instance for the authenticated user
    Cart::instance("wishlist")->restore(Auth::user()->email);
    // Check if user has 2FA enabled
    $user = $request->user();
    if ($user->google2fa_enabled) {
    // Redirect to 2FA verification
    return redirect()->route("2fa.verify.show")->with("status", "Please verify with your 2FA code");
    }
    // Redirect the user
    return redirect()->intended(route("home.index"));
  4. Implement `Logout` Function Logic (Wishlist Store)

    In your `LogoutController` (or custom logout handler), before logging the user out, ensure the current state of the wishlist is saved (stored) to the database against the user'skey.

    // ... inside the custom logout method ...
    // Save the current cart instance (from previous part)
    Cart::instance("cart")->store(Auth::user()->email);
    // Save the current wishlist instance to the database
    Cart::instance("wishlist")->store(Auth::user()->email);
    // Proceed with default logout logic
    Auth::guard("web")->logout();
    $request->session()->invalidate();
    $request->session()->regenerateToken();
    return redirect("/");

Verification and Testing 💖💾

1. Log in as a user and add items to the Wishlist.
2. Log out (the wishlist items should be saved in the `shoppingcart` table, linked by the user'semail).
3. Log in again with the same user credentials. The Wishlist should automatically reload, displaying the previously added items, confirming the persistence functionality is active.