close

How to set and get Cookie in laravel

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error How to set and get Cookie in laravel in php. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

Today I get the following error How to set and get Cookie in laravel in php.

  1. How To Solve set and get Cookie in laravel Error ?

    To Solve set and get Cookie in laravel Error Add at the top of the file add use Symfony\Component\HttpFoundation\Cookie;or simply use Cookie;

  2. How to set and get Cookie in laravel

    To Solve set and get Cookie in laravel Error Add at the top of the file add use Symfony\Component\HttpFoundation\Cookie;or simply use Cookie;

Solution 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;
   }

Solution 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');

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read

Leave a Comment