Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check if a cookie is set in laravel?

I have made a cookie with my controller and this seems to work because if I check my resources in my developer tools it is there. But now I want to do actions with it in my view , but this doesn't 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

this is always returning 'true'

Can anyone help me ?

like image 976
Jim Peeters Avatar asked May 12 '15 19:05

Jim Peeters


3 Answers

change

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

to

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

null, not false is returned when cookie isn't set: https://github.com/illuminate/http/blob/master/Request.php#L363

like image 56
Limon Monte Avatar answered Oct 23 '22 06:10

Limon Monte


you can use this

if($request->hasCookie('cookie_name') != false)
like image 25
hamed Avatar answered Oct 23 '22 06:10

hamed


If you check out 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));
    }

    // ...
like image 6
mtpultz Avatar answered Oct 23 '22 06:10

mtpultz