Search code examples
phpcookieslaravel-5.4

How to set cookie in laravel 5.4?


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.


Solution

  • 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')