wayanjimmy's avatar

I've set default user id = 1 if user is guest , any Idea the best way to implement it ?

I currently working on e-commerce website that allow guest user to add product into cart, So I set the default user id for guest is = 1, currently I just define a method in BaseController just like this

class Controller 
{
    
    protected method getCurrentUserId() 
    {
        if (Auth::guest()) return 1;
        return Auth::id();
    }
}

Any idea about better approaches in Laravel ? I'm just curios how to do it more better, and testable

0 likes
1 reply
jekinney's avatar

I push a random id to the session if guest. I set the cookie for one week from last updated timestamp. Then the guest will have a cart saved for a week or clear cookies.

Runs an artisan console command at midnight on any guest cart that updated at is at or over a week.

I also set Boolean value is_guest so I know to redirect to registration page when they click check out and clean up carts table.

Please or to participate in this conversation.