Laravel

How To Set,Get And Delete Cookies In Laravel.

In this post we will learn, how to create,get and delete cookies in Laravel.

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.

Set Cookies

We can use cookies::make() method to create or set cookies in laravel.

$cookie =   Cookie::queue(Cookie::make('cookieName', 'value', $minutes));

Using the cookies::forever method(), we can set cookies forever.

$cookie = Cookie::forever('name', 'value');

Get Cookies.

We can use cookie::get() method to get cookies in laravel.

$val = Cookie::get('cookieName');

If we want to get all cookies in laravel, we can use cookie::get() method as following:

$get_all_cookies = Cookie::get();

Delete Cookies.

Use Cookie::forget() method to delete or destroy cookies in laravel.

$cookie = Cookie::forget('cookieName');

Check If Cookie Exists.

If we want to check if cookie exists or not. So we can use Cookie::has(‘name’); to check cookies is exist or not.

Cookie::has('cookiename'); OR $request->hasCookie('cookiename').

Add Cookies With Response.

Sometime, we need to add cookies in laravel response. So we can add cookies with response in laravel as following.

return response('view')->withCookie($cookie);

Related Articles

Leave a Reply

Back to top button