How to Create Creating a Cookie in Laravel ?

Answer Posted / rishabh khanna

Cookie can be created by global cookie helper of Laravel.
It is an instance of SymfonyComponentHttpFoundationCookie.
The cookie can be attached to the response using the withCookie() method.
Create a response instance of IlluminateHttpResponse class to call the withCookie() method.
Cookie generated by the Laravel are encrypted and signed and it can’t be modified or read by the client.


Here is a sample code with explanation.

//Create a response instance
$response = new IlluminateHttpResponse('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'));

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is database migration. How to create migration via artisan?

504


What is chunk in laravel?

569


What are policies classes?

520


what is Restful Resource Controllers ?

1252


How to send mail using queue in laravel 5?

502


What do you mean by laravel dusk?

501


How can I learn laravel fast?

520


Is react a library or a framework?

486


How to work with Database in Laravel ?

935


What is the use of php compact function?

552


How to write php code in laravel blade?

522


What is valet used in laravel 5?

550


How to clear cache in laravel?

595


Where will you define laravel's facades?

502


Is valet supports windows or ubuntu environment?

522