本文介绍了如何在Laravel中设置和获取Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置并获取cookie中的值,但是它不起作用:

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()返回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);

推荐答案

设置Cookies

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

获取Cookie

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

这篇关于如何在Laravel中设置和获取Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 15:47