Display current user's (number of) wishlist items in top links, on shared wishlist link

Stefan Bogdanescu
Founder & Senior Architect · 25 September 2026

A common issue that arises when implementing wishlists is the display of item counts in top links after sharing. When a logged-in user visits a shared wishlist link, they expect to see their own active wishlist count rather than the contents of the shared list. Let’s dive into this problem and explore a solution using Laravel.

Understanding the Problem

The core issue lies in how we handle wishlists and share links. Currently, when you visit a shared wishlist link as a logged-in user, the system ignores your active session and instead loads the data from the shared list. This is why you see the item count of the shared list instead of your own.

Identifying the Responsible Code

In a typical Laravel application, this behavior is likely governed by the WishlistsController or a similar controller handling wishlist operations. The key methods to focus on include:

  • show (displaying the shared wishlist)
  • index (displaying the current user’s wishlist)

The Solution Approach

To resolve this issue, we need to modify how Laravel handles the routing and authentication for shared wishlist links. Here’s a step-by-step approach:

  1. Maintain Active User Session: When a logged-in user visits a shared link, ensure that their active session remains intact.

  2. Identify Shared Link Parameter: Extract the unique identifier or slug from the shared link URL that represents the specific wishlist.

  3. Check Authentication Status: Verify if the currently logged-in user has access to view the shared link’s wishlist. This could involve checking against a user_id associated with the shared wishlist.

  4. Redirect/Render Appropriate View: If the logged-in user is authorized, render their own wishlist count in the top link; otherwise, redirect them or display an error message indicating they don’t have access to that wishlist.

Code Implementation Example

Here’s a simplified example of how you might implement this logic in your Laravel controller:

public function showWishlist($slug, Wishlist $wishlist)
{
    $sharedWishlist = $wishlist->where('slug', $slug)->first();

    if (!$sharedWishlist) {
        abort(404); // Handle non-existent wishlists
    }

    // Check if the currently logged-in user has access to this shared wishlist
    if (auth()->user()->cannot('view', $sharedWishlist)) {
        return redirect()->route('wishlists.index')->withErrors([
            'access_denied' => 'You don\'t have permission to view this wishlist.',
        ]);
    }

    // If access is granted, display the logged-in user's wishlist count
    $currentUserWishlistCount = auth()->user()->wishlists->count();

    return view('wishlists.show', [
        'shared_wishlist' => $sharedWishlist,
        'current_user_count' => $currentUserWishlistCount
    ]);
}

Key Takeaways

  • Maintaining the active session of a logged-in user is crucial for displaying their wishlist count.

  • Proper authorization checks are necessary to prevent unauthorized access to shared wishlists.

  • The cannot method provided by Laravel’s authorization system simplifies permission checking within your controllers.

Remember, this example is simplified and may require adjustments based on your application’s specific needs. You might need to adjust the authorization rules, create or modify models, and update views accordingly.

Conclusion

Resolving the wishlist display issue after sharing requires a nuanced understanding of Laravel routing, authentication, and authorization. By maintaining active user sessions, checking access permissions, and rendering the appropriate view based on authorization status, you can ensure that logged-in users see their own wishlist counts in top links when visiting shared wishlist URLs.

This approach demonstrates how to leverage Laravel’s built-in features like route parameters, model relationships, and authorization checks to create a seamless user experience. For more advanced customizations or complex scenarios, consider exploring Laravel Company’s extensive documentation and tutorials on authentication and authorization: Laravel Authentication and Authorization Tutorials.

Implementing this solution will improve your application’s usability by ensuring that users always see their own wishlist item counts when browsing shared lists—a key feature for maintaining a positive user experience in e-commerce platforms.

Our network