Glidarn Glidarn - 1 year ago 152
PHP Question

Set a cookie in Laravel 5+ without response

I'm trying to set a cookie in a function that's not going to return a response. The function is called from a middleware.


// Verify user
$userId = $userService->verify($email, $key);
if ($userId) {
$userService->authenticate($userId, $key);

return $next($request);
} else {
return response('Unauthorized', 401);

is calling $userService->authenticate:

// Get user
$user = $this->getById($id);
// Get user zipcode
$user->zipcode = $this->getZipcodeById($id);
// Set session
Session::set('user', $user);

if (!Cookie::has('user_forever')) {

I have tried to return a response in authenticate but it doen't work. I can't get it to work.. So I appreciates all help!

Thanks in advance

Answer Source

You can use queue() as:

Cookie::queue('key', 'value', 10);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download