jeffersongoncalves/filament-cookie-consent
Installation
composer require jeffersongoncalves/filament-cookie-consent
Publish the config and migrations:
php artisan vendor:publish --provider="JeffersonGoncalves\FilamentCookieConsent\FilamentCookieConsentServiceProvider" --tag="config"
php artisan vendor:publish --provider="JeffersonGoncalves\FilamentCookieConsent\FilamentCookieConsentServiceProvider" --tag="migrations"
php artisan migrate
Register the Widget
Add the cookie consent widget to your Filament dashboard layout (e.g., app/Providers/Filament/AdminPanelProvider.php):
public function panel(Panel $panel): Panel
{
return $panel
->widgets([
\JeffersonGoncalves\FilamentCookieConsent\Widgets\CookieConsent::class,
]);
}
First Use Case
/admin/cookie-consent/settings).Cookie Category Management Define cookie categories in the Filament admin panel under Cookie Consent > Settings:
// Example: Programmatically add a category (e.g., in a service provider)
use JeffersonGoncalves\FilamentCookieConsent\Models\CookieCategory;
CookieCategory::create([
'name' => 'Analytics',
'description' => 'Track user behavior for analytics',
'enabled' => true,
'consent_required' => true,
]);
Conditional Rendering Check consent status in Blade templates or controllers:
// Blade
@if (auth()->check() && auth()->user()->hasCookieConsent())
<!-- Show content that requires consent -->
@endif
// Controller
if ($user->hasCookieConsent('Analytics')) {
// Load analytics scripts
}
Integration with Frontend
Use the hasCookieConsent() helper in JavaScript (via Laravel Mix/Alpine.js):
document.addEventListener('DOMContentLoaded', () => {
if (!window.hasCookieConsent('Marketing')) {
// Disable non-essential scripts
}
});
Dynamic Banner Customization Override the default banner view by publishing the package views:
php artisan vendor:publish --provider="JeffersonGoncalves\FilamentCookieConsent\FilamentCookieConsentServiceProvider" --tag="views"
Modify resources/views/vendor/filament-cookie-consent/banner.blade.php.
Automated Consent for Logged-in Users Use a Filament policy to auto-accept consent for admins:
// app/Policies/CookieConsentPolicy.php
public function autoAccept(User $user)
{
return $user->isAdmin();
}
Third-Party Script Loading Dynamically load scripts based on consent (e.g., Google Analytics):
// app/Helpers/CookieConsentHelper.php
public static function loadScripts()
{
if (hasCookieConsent('Analytics')) {
addScript('https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID');
}
}
Localization Translate cookie messages via Filament’s localization system:
// config/filament-cookie-consent.php
'messages' => [
'accept' => __('cookie_consent.accept'),
'reject' => __('cookie_consent.reject'),
],
Cache Issues
php artisan filament:cache-reset
session:store is configured in config/filament.php for persistent consent tracking.Cookie Domain Mismatch
domain in config/filament-cookie-consent.php matches your app’s domain (e.g., .example.com for subdomains).JavaScript Conflicts
document.addEventListener('filament:init', () => {
// Initialize cookie consent logic
});
Migration Errors
Log Consent Status Add a temporary middleware to log consent checks:
// app/Http/Middleware/LogCookieConsent.php
public function handle($request, Closure $next)
{
\Log::info('Cookie Consent', [
'user_id' => auth()->id(),
'consent' => auth()->user()?->cookieConsent,
]);
return $next($request);
}
Inspect Cookies
Use browser dev tools (Application > Cookies) to verify _cookie_consent is set correctly.
Test in Incognito Always test cookie consent in incognito mode to avoid cached consent states.
Custom Consent Logic
Extend the CookieConsent model to add custom fields:
// app/Models/CustomCookieConsent.php
use JeffersonGoncalves\FilamentCookieConsent\Models\CookieConsent;
class CustomCookieConsent extends CookieConsent
{
protected $casts = [
'custom_field' => 'boolean',
];
}
Event Listeners Listen for consent changes to trigger actions (e.g., analytics reset):
// app/Providers/EventServiceProvider.php
protected $listen = [
\JeffersonGoncalves\FilamentCookieConsent\Events\ConsentUpdated::class => [
\App\Listeners\ResetAnalytics::class,
],
];
API Integration Expose consent status via a custom API route:
// routes/api.php
Route::get('/cookie-consent', function () {
return response()->json([
'consent' => auth()->user()?->cookieConsent,
]);
});
How can I help you explore Laravel packages today?