Level 63
Can you try this ?
return $next($request)->withCookie(cookie()->forever('userKey', Str::random(30)));
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I just created a new Middleware to create a new cookie named 'userKey'.
class UserCookies
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next)
{
if(auth()->user() != null){
return $next($request);
}
return $next($request)->cookie(cookie()->forever('userKey', Str::random(30)));
}
}
Not sure why it isn't working. is there anything that i am missing ?
Please or to participate in this conversation.