Table of Contents
Ever landed on a website and instantly got hit with a “We use cookies” banner? Yeah, it’s everywhere. With privacy rules like GDPR, websites have to be transparent about how they collect data.
If you’re building a Laravel app and haven’t added cookie consent yet, it should be your first priority. You could be breaking laws without even knowing it – or worse, losing user trust.
But don’t stress. Laravel makes this super easy. In this blog, we’ll break down what cookie consent is, why it matters, and how Laravel development experts add it with pre-built packages or do it manually. Let’s start with the basics first!
What is Cookie Consent?
Cookie consent is a way to inform users that your website uses cookies, and it allows them to accept or reject their use. It is the permission a website asks from users before storing or accessing cookies on their devices.
These cookies are small files used to:
- Remember login sessions
- Track user behavior (analytics)
- Show personalized ads or content
Why is it Important?
Many countries have privacy laws like the GDPR in Europe and CCPA in California that require websites to get user consent before using certain types of cookies, especially for:
- Analytics (e.g., Google Analytics)
- Advertising (e.g., Facebook Pixel)
- Tracking across websites
Without consent, collecting this data can be illegal and harm user trust.
Need Help Making Your Site Privacy-Compliant? We can Help!
How to Add Cookie Consent in Laravel With Pre-Built Packages?
Now that you know why cookie consent matters, the next step is adding it to your Laravel app. And if you have not even implemented cookies on your site, explore our guide to add cookies in Laravel.
Once you are done with implementing cookies, there are many Laravel packages that handle everything from showing banners to storing user preferences. Let’s look at a few popular options that make cookie consent setup fast, clean, and compliant.
Using spatie/laravel-cookie-consent
The spatie/laravel-cookie-consent package offers a quick and easy way to display a cookie consent banner in your Laravel application. It’s a lightweight solution that works right out of the box, making it ideal for simple use cases where you just need to inform users and get basic consent.
Installation
To get started, install the package using Composer:
composer require spatie/laravel-cookie-consent
Publish the Assets
Next, publish the package’s configuration and view files so you can customize them if needed:
php artisan vendor:publish --provider="Spatie\CookieConsent\CookieConsentServiceProvider" --tag="cookie-consent-config"
How It Works
To display the cookie banner, simply include it in your main Blade layout:
//in your blade template
@include('cookie-consent::index')
This will render a consent banner at the bottom of your website. When a user clicks Allow cookies, the package stores a cookie to remember their decision, preventing the banner from appearing again.
It’s a hassle-free solution for adding basic cookie consent to your Laravel project without any complex setup.
Using whitecube/laravel-cookie-consent
This package offers more flexibility compared to simpler alternatives. With whitecube/laravel-cookie-consent, you can define multiple cookie categories—such as Essential, Analytics, and Marketing—and let users choose which ones to allow. It’s great for websites that need to offer detailed privacy controls.
Installation
Start by installing the package via Composer:
composer require whitecube/laravel-cookie-consent
Setup & Configuration
Next, publish the necessary files to set up the service provider and configuration:
php artisan vendor:publish --tag=laravel-cookie-consent-service-provider
php artisan vendor:publish --tag=laravel-cookie-consent-config
Customize Views and Translations (Optional)
If you want to modify the appearance or text of the consent modal, publish the views and language files:
php artisan vendor:publish --tag=laravel-cookie-consent-views
php artisan vendor:publish --tag=laravel-cookie-consent-lang
Add to Blade Views
Use the following Blade directives to insert the consent scripts and modal into your layout:
<!DOCTYPE html>
<html lang="en">
<head>
<!-- ... -->
@cookieconsentscripts
</head>
<body>
<!-- ... -->
@cookieconsentview
</body>
</html>
- @cookieconsentscripts: Injects the necessary JavaScript and handles third-party scripts that depend on user consent.
- @cookieconsentview: Displays the consent banner or popup.
This package is a solid choice when you need more than just a basic “accept cookies” button. It gives users meaningful control over their privacy.
Using statikbe/laravel-cookie-consent
The statikbe/laravel-cookie-consent package is built with Bootstrap styling and adds a responsive cookie banner to your Laravel app. One of its standout features is that it lets users revoke their consent at any time, making it a more flexible and user-friendly option.
Installation
To install the package, run the following Composer command:
composer require statikbe/laravel-cookie-consent
Configuration
Before using the package, publish its JavaScript and CSS assets:
php artisan vendor:publish --provider="Statikbe\CookieConsent\CookieConsentServiceProvider" --tag="cookie-public"
Example Usage
To include the styles in your layout, add this to your base.blade.php or any other master layout:
<link rel="stylesheet" type="text/css" href="{{ asset("vendor/cookie-consent/css/cookie-consent.css") }}">
Middleware Setup
For Laravel 11.x and Newer
Add the middleware in your bootstrap/app.php:
// bootstrap/app.php
->withMiddleware(function (Middleware $middleware) {
...
$middleware->web(append: [
...
\Statikbe\CookieConsent\CookieConsentMiddleware::class,
]);
// OR AS AN ALIAS
$middleware->alias([
...
'cookie-consent' => \Statikbe\CookieConsent\CookieConsentMiddleware::class,
]);
})
For Laravel 10.x and Earlier
Update app/Http/Kernel.php like so:
class Kernel extends HttpKernel
{
protected $middleware = [
// ...
\Statikbe\CookieConsent\CookieConsentMiddleware::class,
];
protected $routeMiddleware = [
// ...
'cookie-consent' => \Statikbe\CookieConsent\CookieConsentMiddleware::class,
];
}
And apply the middleware in your routes/web.php:
Route::group([
'middleware' => ['cookie-consent']
], function(){
// ...
});
Add the Banner
To render the consent banner, add the following Blade directive in your layout file:
@include('cookieConsent::index')
This will display a clean, Bootstrap-styled banner at the bottom of the screen. You can also define routes that allow users to change or revoke their cookie preferences later.
This package is ideal if you want built-in Bootstrap support and easy revocation of consent.
Using devrabiul/laravel-cookie-consent
The devrabiul/laravel-cookie-consent package is designed with Tailwind CSS in mind. It’s perfect for Laravel developers who want a modern, lightweight design that blends seamlessly with Tailwind-based UI frameworks.
Installation
To get started, install the package using Composer:
composer require devrabiul/laravel-cookie-consent
Publish Assets
Next, publish the package’s assets so you can use its components:
php artisan vendor:publish --provider="Devrabiul\CookieConsent\CookieConsentServiceProvider"
Usage
Now, include the cookie consent components in your Blade templates.
Add the styles inside the <head> tag:
{!! CookieConsent::styles() !!}
And place the scripts before the closing </body> tag:
{!! CookieConsent::scripts() !!}
Example Template Setup
Here’s a complete sample setup for your layout file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Your Page</title>
{!! CookieConsent::styles() !!}
</head>
<body>
<!-- Your content -->
{!! CookieConsent::scripts() !!}
</body>
</html>
This package offers a sleek, modern UI with minimal setup, which is especially useful if you’re working on Tailwind CSS-based Laravel projects.
How to Add Manual Cookie Consent in Laravel?
Sometimes, you may not want to rely on a pre-built package. Maybe you need full control over the design, or you want a minimal setup without adding dependencies. In such cases, you can manually implement cookie consent in your Laravel application.
Step 1: Create a Cookie Consent Banner
You can add a simple banner using HTML and Blade directly in your layout file (resources/views/layouts/app.blade.php or similar):
@if(!request()->cookie('cookie_consent_accepted'))
<div class="fixed bottom-0 left-0 w-full bg-gray-800 text-white p-4 z-50">
<div class="container mx-auto flex justify-between items-center">
<span>We use cookies to improve your experience. By using our site, you accept our cookie policy.</span>
<form method="POST" action="{{ route('cookie.accept') }}">
@csrf
<button type="submit" class="bg-green-500 text-white px-4 py-2 rounded ml-4">Allow Cookies</button>
</form>
</div>
</div>
@endif
Step 2: Handle the Consent Logic in Routes
Create a new route that will handle the consent action. Add this to your routes/web.php file:
use Illuminate\Support\Facades\Cookie;
Route::post('/cookie/accept', function () {
Cookie::queue('cookie_consent_accepted', true, 525600); // Store for 1 year
return redirect()->back();
})->name('cookie.accept');
Step 3: Style as You Like
You can fully customize the look and feel of the banner using Tailwind CSS, Bootstrap, or your own styles. Since this is manual, you’re free to make it match your brand perfectly.
Benefits of Manual Implementation
- Full control over UI/UX
- No third-party dependencies
- Easy to extend with categories (e.g., analytics, marketing)
- Works with any frontend framework or setup
Things to Keep in Mind
- You need to handle cookie revocation manually if required.
- You are responsible for tracking and managing additional consent categories (if needed).
- It’s your job to ensure compliance with local regulations (like GDPR or CCPA).
Manual implementation is great for simple websites or those requiring custom consent behavior without relying on a package.
Best Practices for Cookie Consent in Laravel
Implementing cookie consent isn’t just about putting up a banner. It’s about respecting user privacy, following legal regulations, and ensuring a smooth user experience. Here are some best practices Laravel developers should follow when handling cookie consent:
1. Follow Legal Regulations
Privacy laws like GDPR and CCPA set strict rules for how websites handle user data. GDPR requires upfront consent before setting up non-essential cookies, while CCPA focuses more on giving users the option to opt out.
If your audience spans multiple regions, make sure your implementation aligns with the strictest applicable standard. And don’t forget to include links to your Privacy Policy and Cookie Policy for full transparency.
2. Differentiate Cookie Categories
Not all cookies are created equal. Categorize them clearly:
- Essential Cookies: Needed for site functionality (e.g., session cookies).
- Analytics Cookies: Help you understand visitor behavior (e.g., Google Analytics).
- Marketing Cookies: Used for advertising and remarketing.
Allow users to selectively accept or reject categories when possible.
3. Give Users Full Control
Consent shouldn’t be a one-time event. Users should be able to manage or revoke their choices at any time. You can make this easy by adding a “Manage Cookies” or “Cookie Settings” link in your footer or profile section.
If someone changes their preferences, stop using cookies that are disallowed right away. With Laravel, you can conditionally load scripts based on user consent — no extra complexity is needed.
4. Design a Clean, User-Friendly Interface
A messy or aggressive cookie banner can frustrate users and hurt engagement. Keep the language simple. Use clear buttons like “Accept All,” “Reject,” or “Manage Preferences.”
Make sure the banner doesn’t block important content and that it works just as well on mobile as it does on desktop. A clean design not only looks better but also encourages users to interact with it honestly.
5. Test Across Browsers and Devices
Your consent setup might look great on Chrome, but what about Safari or Firefox? Make sure it behaves consistently across modern browsers and screen sizes.
Try accepting and rejecting cookies on desktop and mobile to confirm that your app respects those choices properly and doesn’t store anything without consent.
6. Store and Respect Consent Properly
Use Laravel’s Cookie::queue() method to save user consent safely. Set a reasonable expiry — most developers go with 6 to 12 months. Also, make sure you’re not dropping any non-essential cookies before the user has given permission. This one step alone keeps you clear of most compliance issues.
7. Log Consent if Necessary
In some cases, it’s a good idea (or legally required) to log user consent:
- Timestamp of when consent was given
- IP address (if permitted)
- Type of consent (e.g., full, partial)
This can be useful during audits or compliance checks.
8. Keep It Updated
Privacy laws change. Browser policies evolve. Laravel packages get updates. So should your implementation. Make it a habit to review your cookie consent setup during major site updates or quarterly audits. Staying current ensures you’re not unintentionally falling out of compliance.
We Build Secure & Privacy-First Laravel Websites. Let’s Build Yours!
FAQs on Adding Cookie Consent in Laravel
How to add cookies in Laravel?
Use Cookie::queue() to add a cookie in Laravel. Example: Cookie::queue(‘name’, ‘value’, $minutes); The cookie will be sent in the next response and stored in the user’s browser.
How to secure cookies in Laravel?
Use HTTPS and set cookie options like secure, HttpOnly, and SameSite. You can do this in config/session.php or when creating cookies.
How does Laravel encrypt cookies?
Laravel encrypts cookies automatically. It uses a secret key from your .env file, so you don’t have to do anything extra.
Conclusion
Cookie consent isn’t just a checkbox; it’s about respecting privacy, building trust, and staying legally safe. Laravel makes it simple with built-in tools and helpful packages that save time and effort. Whether you use a ready-made solution or a custom setup, the key is to stay transparent and give users control.
Keep things clear, follow the rules, and update your setup as laws evolve. Cookie consent is not just about compliance; it is part of a better user experience. And Laravel gives you everything you need to get it right.
If you still need help with setting up cookies or want to make your site GDPR-compliant, hire our Laravel developers.