digifactory/laravel-cookie-consent

Easily deal with cookie consent in your Laravel applications
16,670 9
Install
composer require digifactory/laravel-cookie-consent
Latest Version:v5.0.1
PHP:^8.3
License:MIT
Last Updated:Apr 19, 2026
Links: GitHub  ·  Packagist
Maintainer: digifactory

Laravel Cookie Consent

Latest Version on Packagist MIT Licensed run-tests Quality Score StyleCI Total Downloads

This package makes dealing with cookie consent in your Laravel app and Blade views a piece of cake. By default the package uses Cookiebot as its 'consent provider'. It doesn't replace the Cookiebot's (or any other consent provider's) JavaScript implementation. Laravel v6.5 is required, this is necessary for the unless Blade directive.

Installation

You can install the package via composer:

composer require digifactory/laravel-cookie-consent

You can publish the config file:

php artisan vendor:publish --provider="DigiFactory\CookieConsent\CookieConsentServiceProvider" --tag="config"

Usage

By default cookie consent is enabled. This means for all conditionals we use the consent provider to check if the user has given consent. You can disable cookie consent by creating an environment variable COOKIE_CONSENT_ENABLED and set it to false. If cookie consent is disabled all checks will return true, so all cookies are allowed as if the user has given consent for all types of cookies.

Blade

You can use the following Blade directives in your views:

  • cookieConsentNecessary
  • unlesscookieConsentNecessary
  • elsecookieConsentNecessary
  • endcookieConsentNecessary
  • cookieConsentPreferences
  • unlesscookieConsentPreferences
  • elsecookieConsentPreferences
  • endcookieConsentPreferences
  • cookieConsentStatistics
  • unlesscookieConsentStatistics
  • elsecookieConsentStatistics
  • endcookieConsentStatistics
  • cookieConsentMarketing
  • unlesscookieConsentMarketing
  • elsecookieConsentMarketing
  • endcookieConsentMarketing

You can do something like this:

@cookieConsentStatistics
<script>
    <!-- Put your analytics code here! -->
</script>
@endcookieConsentStatistics

@cookieConsentMarketing
    <iframe width="560" height="315" src="https://www.youtube.com/embed/fzQSE_3eLKk" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
@else
    Please allow marketing cookies to view this video. Click <a href="https://github.com/digifactory/laravel-cookie-consent/blob/HEAD/javascript: Cookiebot.renew()">here</a> to renew or change your cookie consent.
@endcookieConsentMarketing

@unlesscookieConsentPreferences
    We cannot save your preferences because you did not allow preference cookies.
@endcookieConsentPreferences

PHP

Or check for given consent in your PHP code:

CookieConsent::forNecessary();
CookieConsent::forPreferences();
CookieConsent::forStatistics();
CookieConsent::forMarketing();

If you don't want to use the Facade then you can use app('cookie-consent'):

app('cookie-consent')->forNecessary();
app('cookie-consent')->forPreferences();
app('cookie-consent')->forStatistics();
app('cookie-consent')->forMarketing();

Implementing your own consent provider

You consent provider should implement the ConsentProvider contract:

<?php

namespace DigiFactory\CookieConsent\Contracts;

interface ConsentProvider
{
    public function forNecessary(): bool;

    public function forPreferences(): bool;

    public function forStatistics(): bool;

    public function forMarketing(): bool;
}

You can override the default provider in the config:

<?php

return [
    /**
     * By default this package uses Cookiebot to determine if certain
     * types of cookies are allowed. Of course you can use a provider
     * of your own, your provider should implement the ConsentProvider
     * interface.
     */
    'provider' => \DigiFactory\CookieConsent\Providers\Cookiebot::class,
];

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email mark@digifactory.nl instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.

Related Packages

draperstudio/laravel-cookie-consent

Easy drop-in Cookie Consent (Silktide) for Laravel 5

373 3
digifactory/filament-wildcard-login

Allow all e-mail addresses for a specific domain name to login to a generic acco...

1,827 11
digifactory/laravel-svg-fixer-middleware

Fixes your uploaded SVG files before validating through the Laravel validator

35,273 5
digifactory/laravel-pull-production-data

Pull your production storage folder and database to your local environment

17,508 12
jeffersongoncalves/laravel-cookie-consent

This Laravel package provides a simple and elegant way to implement cookie conse...

15,186 5