Laravel cookies. In this tutorial, we will show you how to use cookies in laravel.
Before we will show you how to get, set and delete all cookies in laravel. And as well as how to check cookies exist or not. You must know what is cookies?
Cookies are a small data file, which is stored in the remote browser. And by the help of cookies tracking/identifying return users in web applications.
Laravel Cookies Set, Get and Delete
You can see below how to get, set and delete all laravel cookies:
Laravel Set Cookies
You can use cookies::make() method to create or set cookies in laravel:
$cookie = Cookie::make('name', 'value', 120);
Using the cookies::forever method(), you can set cookies forever:
$cookie = Cookie::forever('name', 'value');
Laravel Get Cookies
You can use cookie::get() method to get cookies in laravel:
$val = Cookie::get('cookieName');
If you want to get all cookies in laravel, you can use cookie::get() method as following:
$get_all_cookies = Cookie::get();
Laravel Delete Cookies
Use Cookie::forget() method to delete or destroy cookies in laravel:
$cookie = Cookie::forget('cookieName');
Laravel Check if Cookie Exists
If you want to check if cookie exists or not. So you can use Cookie::has(‘name’); to check cookies is exist or not.
Cookie::has('cookiename'); OR $request->hasCookie('cookiename')
Add Cookies With Response
Sometime, you need to add cookies in laravel response. So you can add cookies with response in laravel as following:
return response('view')->withCookie($cookie);