如何检查是否在laravel中设置了cookie?

时间:2022-10-18 17:48:09

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:

我用我的控制器制作了一个cookie,这看起来很有效,因为如果我在开发人员工具中检查我的资源就可以了。但是现在我想在我的视图中用它做一些动作,但这似乎不起作用,这是我在视图中使用的代码:

@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 ?

谁能帮我 ?

3 个解决方案

#1


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

null未设置cookie时返回false:https://github.com/illuminate/http/blob/master/Request.php#L363

#2


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

如果你现在检查Cookie类,你可以使用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));
    }

    // ...

#3


you can use this

你可以用它

if($request->hasCookie('cookie_name') != false)

#1


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

null未设置cookie时返回false:https://github.com/illuminate/http/blob/master/Request.php#L363

#2


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

如果你现在检查Cookie类,你可以使用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));
    }

    // ...

#3


you can use this

你可以用它

if($request->hasCookie('cookie_name') != false)