How to Create Creating a Cookie in Laravel ?



How to Create Creating a Cookie in Laravel ?..

Answer / 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

More Laravel PHP Framework Interview Questions

List out the plateforms laravel homestead supports?

0 Answers  


How to use traits in laravel?

0 Answers  


What is the use of php compact function?

0 Answers  


What is http middleware?

0 Answers  


How do I setup localization for an application?

0 Answers  


What is namespace laravel?

0 Answers  


Explain the bindings and singletons properties?

0 Answers  


What is orm in laravel?

0 Answers  


What is laravel dependency injection?

0 Answers  


How to get JSON Response on Laravel ?

1 Answers  


What are helper functions and name 10 with their purposes?

0 Answers  


What is faker in laravel?

0 Answers  


Categories