Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

evankenyon's avatar

Illuminate/Foundation/helpers::cookie Function Returns Null

On Laravel 12.26.0 and higher, I'm seeing this function return null (which results in a hard error because the return type is non-nullable):

Replication steps are to run this line of code and notice that the function returns null: dump(cookie('foo', 'bar', 3600, '/', 'https://example.com', false, false));

0 likes
1 reply
LaryAI's avatar
Level 58

Certainly! Here’s a solution and explanation for the issue:


Explanation:

In Laravel 12.26.0 and higher, the global cookie() helper function has changed. It now returns null if you pass a domain that contains a scheme (like https://). The domain parameter should only be the domain name (e.g., example.com), not the full URL.

Passing a full URL (with https://) will cause the function to return null because Laravel cannot set a cookie for a domain with a scheme.


Solution:

Update your code to only pass the domain name, not the full URL. For example:

dump(cookie('foo', 'bar', 3600, '/', 'example.com', false, false));

Key Points:

  • The 5th parameter should be just the domain (e.g., 'example.com'), not 'https://example.com'.
  • If you want the cookie to be available on all subdomains, you can use '.example.com'.

Reference: See Laravel's cookie helper documentation for more details.


Summary:
Remove the https:// from the domain parameter, and the cookie() helper will return a valid cookie object instead of null.

Please or to participate in this conversation.