In short, a cookie is a tiny portion of data from a website saved within a web browser, and the website can later retrieve this data.
In this detailed post, you will learn how to Set, Get and Delete cookies in the Laravel application using the built-in Laravel Cookie facade.
A cookie is a piece of information that resides inside the web browser. Cookies are used to store our personal information; in order to save data in cookies, you have to set cookies first in Laravel.
We are also going to look at other fundamentals, such as: how to get data from cookies in Laravel not only but also how to delete cookies in laravel.
All cookies created by the Laravel framework are safe and secure. Its tightened security doesn’t let hackers steal your precious information.
Cookies in laravel are encrypted and signed with an authentication code, meaning they will be considered invalid if the client has changed them.
Laravel Set Cookie with HTTP Request
Laravel offers two conventional ways to set up the data on the browser using cookie.
We can add the information as cookie using the Cookie Facade in Laravel.
namespace AppHttpControllers;
use Cookie;
class UserProfileController extends Controller
{
public function addBrowserCookie()
{
Cookie::queue('user-cookie', 'Add Cookie from cnn.com', 120);
return response()->json(['Cookie has been successfully set.']);
}
}
There is another approach to set cookie is by using the Http Request Response service.
namespace AppHttpControllers;
use IlluminateHttpRequest;
class UserProfileController extends Controller
{
public function addBrowserCookie(Request $request)
{
return response()->json(['Cookie has been successfully set.'])->cookie(
'user-cookie-two', 'Cookie demo two', 120
);
}
}
Laravel Get Cookie Example
We will show you how to extract the cookie data using Cookie::get()
method; ensure that you use the cookie get method and pass the cookie name.
namespace AppHttpControllers;
use IlluminateHttpRequest;
use Cookie;
class UserProfileController extends Controller
{
public function getBrowserCookie()
{
$cookieRes = Cookie::get('user-cookie');
dd($cookieRes);
}
}
Laravel Delete Cookie Example
If you want to clear the cookies in laravel, you can use the Cookie::forget() method. Ensure that you added the cookie names correctly; here is the code example.
namespace AppHttpControllers;
use IlluminateHttpRequest;
use Cookie;
class UserProfileController extends Controller
{
public function deleteBrowserCookie()
{
Cookie::forget('user-cookie');
Cookie::forget('user-cookie-two');
dd('Cookie has been successfully deleted.');
}
}
Conclusion
Cookies are ideally set when you have to save certain data in the web browser. Cookies help perform a certain task based on the set data or information.
This tutorial taught us how to set, get and delete cookies in a user’s computer web browser.
We saw how to use cookies and Http Request Facade to set cookies in the laravel app, how to retrieve cookie values in laravel and most importantly, how to clear cookies data in the laravel application.