How to set cookie in laravel 5.4?

邮差的信 提交于 2019-12-11 04:04:26

问题


I have this function in laravel 5.4 but I cant get anything from cookie.

Cookie::queue('currentLang', 'heb', 999999999);

echo  $request->cookie('currentLang');

But I am getting some long string and not what I set.


回答1:


Laravel has a cookie helper!

/**
 * Create a new cookie instance.
 *
 * @param  string  $name
 * @param  string  $value
 * @param  int     $minutes
 * @param  string  $path
 * @param  string  $domain
 * @param  bool    $secure
 * @param  bool    $httpOnly
 * @return \Symfony\Component\HttpFoundation\Cookie
 */
function cookie($name = null, $value = null, $minutes = 0, $path = null, $domain = null, $secure = false, $httpOnly = true)
{
    $cookie = app(CookieFactory::class);

    if (is_null($name)) {
        return $cookie;
    }

    return $cookie->make($name, $value, $minutes, $path, $domain, $secure, $httpOnly);
}

then use:

Cookie::get('$name')



回答2:


use Illuminate\Support\Facades\Cookie;

Cookie::queue('currentLang', 'heb', 999999999);

echo Cookie::get('currentLang');


来源:https://stackoverflow.com/questions/44870016/how-to-set-cookie-in-laravel-5-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!