How to use laravel cookies – Get, Set, Delete with example

Spread the love

Hey Guys,

We’ll delve into a practical example of leveraging Laravel cookies. We’ll comprehensively address key aspects, including setting, retrieving, and deleting cookies using Laravel. Throughout this tutorial, I’ll offer a clear and concise demonstration of how to work with cookies within the Laravel framework.

Discover real-world applications of Laravel cookies in the following examples. These illustrations are relevant and applicable to various Laravel versions, including 6, 7, 8, and 9.

Laravel cookies Examples

Laravel Set Cookies : –

Example 1: Cookie Set Using Cookie Facade

Cookie::queue('Cookie-name', 'Cookie-value', $Cookie-time-in-minutes);

Example:

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Cookie;
  
class CookieController extends Controller
{
  
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function setCookie()
    {
        Cookie::queue('test-cookie', 'Setting Cookie from itcodestuff.com', 120);
  
        return response()->json(['Cookie set successfully.']);
    }
}

Example 2: Cookie Set with Request Response

cookie('Cookie-name', 'Cookie-value', $Cookie-time-in-minutes);

Example:

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
  
class CookieController extends Controller
{
  
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function setCookie()
    {
        return response()->json(['Cookie set successfully.'])->cookie(
            'test-cookie-2', 'Demo 2', 120
        );
    }
}

Laravel Get Cookies :-

Also Read: How To Create Repeater Field Using Bootstrap

Example 1: Cookie Get Using Cookie Facade

Cookie::get('Cookie-name')

Example:

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Cookie;
  
class CookieController extends Controller
{
  
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function getCookie(Request $request)
    {
        $value = Cookie::get('test-cookie');
        dd($value);
    }
}

Example 2: Cookie Get with Request Response

$request->cookie('Cookie-name')

Example:

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
  
class CookieController extends Controller
{
  
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function getCookie(Request $request)
    {
        $value2 = $request->cookie('test-cookie-2');
        dd($value2);
    }
}

Laravel Delete Cookies :-

Cookie::forget('Cookie-name')

Example:

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Cookie;
  
class CookieController extends Controller
{
  
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function deleteCookie()
    {
        Cookie::forget('test-cookie');
        Cookie::forget('test-cookie-2');
  
        dd('Cookie removed successfully.');
    }
}

Now that you have a comprehensive understanding of managing cookies in your Laravel application, including how to set, retrieve, and delete them, you can confidently optimize your web development experience. This knowledge will undoubtedly prove invaluable to you.

Embrace the elite realm of Laravel insights – your compass to the best Laravel blog awaits.

Leave a Comment