How to set cookies in laravel 5 independently inside controller

113,199

Solution 1

You may try this:

Cookie::queue($name, $value, $minutes);

This will queue the cookie to use it later and later it will be added with the response when response is ready to be sent. You may check the documentation on Laravel website.

Update (Retrieving A Cookie Value):

$value = Cookie::get('name');

Note: If you set a cookie in the current request then you'll be able to retrieve it on the next subsequent request.

Solution 2

If you want to set cookie and get it outside of request, Laravel is not your friend.

Laravel cookies are part of Request, so if you want to do this outside of Request object, use good 'ole PHP setcookie(..) and $_COOKIE to get it.

Solution 3

You are going right way my friend.Now if you want retrive cookie anywhere in project just put this code $val = Cookie::get('COOKIE_NAME'); That's it! For more information how can this done click here

Solution 4

Here is a sample code with explanation.

 //Create a response instance
 $response = new Illuminate\Http\Response('Hello World');

 //Call the withCookie() method with the response method
 $response->withCookie(cookie('name', 'value', $minutes));

 //return the response
 return $response;

Cookie can be set forever by using the forever method as shown in the below code.

$response->withCookie(cookie()->forever('name', 'value'));

Retrieving a Cookie

//’name’ is the name of the cookie to retrieve the value of
$value = $request->cookie('name');
Share:
113,199
Admin
Author by

Admin

Updated on May 09, 2020

Comments

  • Admin
    Admin almost 4 years

    I want to set cookies in Laravel 5 independently

    i.e., Don't want to use

    return response($content)->withCookie(cookie('name', 'value'));

    I just want to set cookie in some page and retrieve in some other page

    Creation can be like this

    $cookie = Cookie::make('name', 'value', 60);

    But how can i retrieve those cookies in some controller itself ?

  • The Alpha
    The Alpha almost 9 years
    Please check the documentation (link given in the answer), you'll find everything there.
  • Dan Jones
    Dan Jones over 7 years
    He asked for Laravel 5. This example is for Laravel4.
  • Dan Jones
    Dan Jones over 7 years
    Doesn't work on my Laravel 5. That Cookie class seems to be gone.
  • The Alpha
    The Alpha over 7 years
    @DanJones, I'll check soon, which release (5.0/5.2/5.2...) of version - 5 you are on?
  • Dan Jones
    Dan Jones over 7 years
    5.0.35 Although, I'm still pretty new to Laravel, so there may be something I'm missing.
  • Nil
    Nil over 7 years
    Hi, I use $value=Cookie::get('name'); but when I echo $value it shows me blank, before this I set cookie by using Cookie::queue('name',$name); Plz help me to resolve this
  • Jigs Virani
    Jigs Virani over 7 years
    $value will be null if cookie not present. see this link: laravel-recipes.com/recipes/49/…
  • Jeffz
    Jeffz over 6 years
    @Jigs Virani You are wrong. If you want to set cookie and get it in Laravel outside of request, Laravel is not your friend. Even link you gave says; "This is basically the same as Request::cookie()". So, Nil is right to complain.
  • Kokodoko
    Kokodoko over 4 years
    I'm getting "maximum execution time exceeded" when using this.
  • t q
    t q over 3 years
    setcookie() is not working, are there any settings that may be interfering this?