How to set and get Cookie in laravel

2020-06-16 03:39发布

问题:

I would like to set and get value in cookie but it doesn't work:

    Cookie::queue('online_payment_id', "1", 15);

    $value = Cookie::get('online_payment_id');
    dd($value);

dd() returns null;


I used below way but I got this message:

Method cookie does not exist.

    request()->cookie('online_payment_id');

    $value = response()->cookie('online_payment_id', "1", 15);
    dd($value);

回答1:

Set Cookies

 public function setCookie(Request $request){
      $minutes = 60;
      $response = new Response('Set Cookie');
      $response->withCookie(cookie('name', 'MyValue', $minutes));
      return $response;
   }

Get Cookie

   public function getCookie(Request $request){
      $value = $request->cookie('name');
      echo $value;
   }


回答2:

Add at the top of the file add use Symfony\Component\HttpFoundation\Cookie;or simply use Cookie;

To Generating Cookie Instances

    $cookie = cookie('name', 'value', $minutes);
    return response('Hello World')->cookie($cookie);

Retrieving Cookies From Requests you can use Request be sure you use Request $request in you method.

    $value = $request->cookie('name');


回答3:

Even if you carefully follow the Laravel documentation regarding how to set cookies, you may end up going crazy for hours (as I did today) because you just can't get your cookies set!

I finally discovered why my cookies weren't being set... I was also using the dump() function to display data while I tested the code. The dump() function sends output to the browser, which requires headers to be sent. Cookies also have to be sent with the headers, so if you're using dump(), your cookies will never be sent!

I hope this helps others who will very likely run into this situation.



回答4:

Like everything else in laravel there are many ways of set/get cookies. The cookie will automatically be added to the outgoing response.

    $value = 1;
    $minutes = 15;
    Cookie::queue($online_payment_id, $value, $minutes);

In order to get the cookie you can use the

    request()->cookie($online_payment_id);


回答5:

public function setCookie(Request $request) {

    Cookie::queue('name', $request->test, 10);

    return view('home');
 }

Here Cookie::queue($cookie_name, $data, $life_time_of_this_cookie);

and simply print cookie where you want to show data.

 {{ Cookie::get('name') }}


回答6:

There are several methods to set and get cookies in laravel.

Official documentation says Cookies

I usually ended up this way

$response = new \Illuminate\Http\Response(view('welcome'));
$response->withCookie(cookie('test_cookie', $request->test_cookie, 45000));
return $response;

You can also use CookieJar

Refer CookieJar



回答7:

to show all cookies

request()->cookie();

to get specific item

request()->cookie('itm_name');

to set item

request()->cookie('item_name', 'item_value', Min_how_long_it_will_alive);


回答8:

Try this:

public function setCookie(Request $request){

 $cookie_name = "user";
 $cookie_value = "value";
 setcookie($cookie_name,$cookie_value, time() + (86400 * 30), "/"); //name,value,time,url

 dd($_COOKIE['user']);

}