1

I've been using Laravel from command prompt in Windows 10, but the difficulty of switching between projects has made me switch to using Homestead. However, in a new project I have started there, I can't for the life of me get cookies to persist. Here is my current code (for debugging this problem only):

use Illuminate\Support\Facades\Cookie;

// ......

public function __construct(Request $request) {
    $customer_id = Cookie::get('customer_id');
    if(!$customer_id) {
        Cookie::queue('customer_id', time(), 3600);
    }
    dd($customer_id);
}

Expected output: On consecutive page loads, the visitor will see the same unix timestamp they initially opened the page at (I understand this is not a good way of handling it, again, this is just for reproducing the error.)

Reality: Every pageload will produce a different timestamp.

I've looked up as many discussions as I could find. Solutions that I tried:

  • Using the Route method of declaring cookies
  • Using good-old PHP setcookie
  • Using Cookie:make, and Cookie:forever
  • Adding 'customer_id' in the exceptions among EncryptCookies
  • Placing the route in the web middleware
  • Erasing php artisan cache, restarting vagrant
  • Making the session folder editable through chmod

Yet still, after applying all the above, the cookie is still gone after every page load.

Since I had no prior problem like this through Xampp's PHP, I have to assume there is a (hopefully) trivial and obvious problem with Vagrant that I don't yet know. Any advice is appreciated!

Andrew
  • 827
  • 2
  • 6
  • 14
  • In what class are you using this? – Jonathan Apr 03 '20 at 21:11
  • @Jonathan It is in a custom controller that I created through php artisan. There is basically nothing fancy in it. – Andrew Apr 03 '20 at 21:13
  • What are you trying to do with the Cookie, exactly? What do you need further access to with (what surely should be) `$customerId = Cookie::get('customer_id');`? – Jonathan Apr 03 '20 at 21:34
  • @Jonathan You're right, the code I attached was quite wonky, I updated it. In my defense, it's just a quick reproducible example I created, and it's about the 20th iteration by now, which makes me assume that this is rather a problem with setting up the Homestead server correctly. - The basic idea is: when a new customer opens the page, they are assigned a unique id in a cookie, by which they can continue filling out the same multipage questionaire even if they close the browser and return later. – Andrew Apr 03 '20 at 21:42

1 Answers1

2

Queued cookies are only sent with responses, so be sure that your controller function does return one.

use Illuminate\Support\Facades\Cookie;

// ......

public function __construct(Request $request) {
    $customer_id = Cookie::get('customer_id');
    if(!$customer_id) {
        Cookie::queue('customer_id', time(), 3600);
    }
}

public function foo() {
    ...

    return response('some text');
}

Also, if using some kind of api you have to add a middleware to include the cookies on the response. See Laravel 5.4 - Cookie Queue

Daniel Costa
  • 380
  • 1
  • 9
  • Wow, I can't believe it was this simple, but now it works... and here I was bouncing at this problem all afternoon! Thank you, Daniel! So in the end if I understand correctly, my problem was that I just never left the constructor, and always dd-d or exited out with something. Now I feel dumb. Thanks again! – Andrew Apr 03 '20 at 21:55