How to Set, Get and Delete Cookies in Laravel 10 App

Last Updated on by in Laravel

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 10 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.

<?php
  
namespace App\Http\Controllers;
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.

<?php
  
namespace App\Http\Controllers;
use Illuminate\Http\Request;
  
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.

<?php
  
namespace App\Http\Controllers;
use Illuminate\Http\Request;
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.

<?php
  
namespace App\Http\Controllers;
use Illuminate\Http\Request;
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

How to Set, Get and Delete Cookies in Laravel App

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.