How to check if cookie is set in laravel?

I made a cookie with my controller, and it seems to work, because if I check my resources in my developer tools, it is. But now I want to do actions with it, in my opinion, but this does not seem to work, this is the code I used in my view:

@if (Cookie::get('cookiename') !== false)
    <p>cookie is set</p>
@else
    <p>cookie isn't set</p>
@endif

it always returns true

Can anybody help me?

+4
source share
2 answers

change

@if (Cookie::get('cookiename') !== false)

to

@if (Cookie::get('cookiename') !== null)

null, but does not falsereturn when the cookie is not set: https://github.com/illuminate/http/blob/master/Request.php#L363

+7
source

If you check the cookie class now, you can use Cookie::has('cookieName');

class Cookie extends Facade
{
    /**
     * Determine if a cookie exists on the request.
     *
     * @param  string  $key
     * @return bool
     */
    public static function has($key)
    {
        return ! is_null(static::$app['request']->cookie($key, null));
    }

    // ...
+2
source

All Articles