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

Andreas94's avatar

Laravel does not read the cookie saved in plain text

I'm trying to save Tailwind's darkmode cookies, but Laravel then won't read the cookie to me.

I used JavaScript to save the cookies:

function setCookie(name, value) {
  var d = new Date();
  d.setTime(d.getTime() + (365*24*60*60*1000));
  var expires = "expires=" + d.toUTCString();
  document.cookie = name + "=" + value + ";" + expires + ";path=/";
}
themeToggleBtn.addEventListener('click', function() {
// if set via local storage previously
if (localStorage.getItem('color-theme')) {
if (localStorage.getItem('color-theme') === 'light') {
    document.documentElement.classList.add('dark');
    localStorage.setItem('color-theme', 'dark');
    setCookie('theme', 'dark');
} else {
    document.documentElement.classList.remove('dark');
    localStorage.setItem('color-theme', 'light');
    setCookie('theme', 'light');
}
// if NOT set via local storage previously
} else {
if (document.documentElement.classList.contains('dark')) {
    document.documentElement.classList.remove('dark');
    localStorage.setItem('color-theme', 'light');
    setCookie('theme', 'light');

} else {
    document.documentElement.classList.add('dark');
    localStorage.setItem('color-theme', 'dark');
    setCookie('theme', 'dark');
}
}
});

However, when I use:

$theme = \Cookie::get('theme');

Laravel does not interpret the content correctly. I checked the Cookie and I have the plain text "Dark", and not encrypted as Laravel saves them using Cookie::queue.

How do I get Laravel to read it?

0 likes
1 reply
tykus's avatar
tykus
Best Answer
Level 104

Add the theme cookie to the except array in EncryptCookies middleware

class EncryptCookies extends Middleware
{
    /**
     * The names of the cookies that should not be encrypted.
     *
     * @var array<int, string>
     */
    protected $except = [
      'theme',
    ];
}

Please or to participate in this conversation.