How to set and get Cookie in laravel

To set a cookie in Laravel, you can use the Cookie facade.

Here is an example of how to set a cookie:

<?php

use Illuminate\Support\Facades\Cookie;

// Set a cookie that expires in 30 minutes
Cookie::queue(Cookie::make('name', 'value', 30));

To retrieve a cookie value, you can use the request()->cookie() method in your controller or middleware.

Watch a course Learn object oriented PHP

Here is an example of how to get a cookie value:

<?php

use Illuminate\Http\Request;

public function getCookie(Request $request)
{
    $value = $request->cookie('name');
    // Do something with the cookie value
}

Alternatively, you can use the Cookie facade to retrieve a cookie value:

<?php

use Illuminate\Support\Facades\Cookie;

$value = Cookie::get('name');