Install the Package:
composer require visualbuilder/user-consent:^5.0
Ensure your composer.json locks to compatible Filament (5.x) and Laravel (11.x/12.x) versions.
Publish Assets: Run migrations and config:
php artisan vendor:publish --tag="user-consent-migrations"
php artisan vendor:publish --tag="user-consent-config"
php artisan migrate
Optionally publish views for customization:
php artisan vendor:publish --tag="user-consent-views"
Configure Consent Options:
Edit config/user-consent.php to define consent types (e.g., marketing, analytics):
return [
'options' => [
'marketing_emails' => [
'label' => 'Marketing Emails',
'description' => 'Receive promotional emails.',
'required' => false,
],
'analytics_tracking' => [
'label' => 'Analytics Tracking',
'description' => 'Allow tracking of your usage.',
'required' => true,
],
],
];
Integrate with Filament Registration:
Extend Filament’s registration form to include consent fields. Example in a custom Registration class:
use VisualBuilder\UserConsent\Concerns\HandlesUserConsent;
class CustomRegistration extends Filament\Panels\Registration
{
use HandlesUserConsent;
}
Test the Flow:
consents table.HandlesUserConsent trait in Filament’s Registration class to auto-attach consent fields.// app/Filament/Panels/CustomRegistration.php
use VisualBuilder\UserConsent\Concerns\HandlesUserConsent;
class CustomRegistration extends Filament\Panels\Registration
{
use HandlesUserConsent;
protected function getConsentOptions(): array
{
return config('user-consent.options');
}
}
getConsentOptions() to dynamically fetch options from a database if needed.Consent resource.php artisan make:filament-resource Consent
Then extend the resource to query consents:
// app/Filament/Resources/ConsentResource.php
public static function getRelations(): array
{
return [
'user' => UserResource::getEloquentQuery(),
];
}
// In a Filament UserResource
public static function getPages(): array
{
return [
'consents' => Pages\ConsentsPage::route('/consents'),
];
}
Action to trigger consent updates for existing users.// In a Filament UserResource Table
public static function getTableActions(): array
{
return [
Action::make('requestConsent')
->label('Request Consent Update')
->action(function (User $user) {
// Trigger email or redirect to consent form
return redirect()->route('filament.admin.pages.user-consents.show', ['user' => $user]);
}),
];
}
php artisan vendor:publish --tag="user-consent-views"
resources/views/vendor/user-consent/emails/consent-notification.blade.php.ConsentNotification class to modify logic:
// app/Providers/UserConsentServiceProvider.php
use VisualBuilder\UserConsent\Mail\ConsentNotification;
class UserConsentServiceProvider extends ServiceProvider
{
public function boot(): void
{
ConsentNotification::macro('customize', function ($user) {
$this->subject("Updated Consents for {$user->name}");
// Add custom logic
});
}
}
// app/Models/ConsentOption.php
class ConsentOption extends Model
{
protected $fillable = ['label', 'description', 'required'];
}
Then override getConsentOptions():
protected function getConsentOptions(): array
{
return ConsentOption::all()->pluck('label', 'key')->toArray();
}
// app/Providers/Filament/AdminPanelProvider.php
public function panel(Panel $panel): Panel
{
$panel
->middleware([
\VisualBuilder\UserConsent\Http\Middleware\CheckPanelConsent::class,
])
->consentOptions(function () {
return config('user-consent.options.panel_'.request()->panel);
});
}
required based on user roles:
protected function getConsentOptions(): array
{
$options = config('user-consent.options');
if (auth()->user()->isAdmin()) {
foreach ($options as &$option) {
$option['required'] = false;
}
}
return $options;
}
// Dispatch a job to update consents for a user batch
UpdateConsentsJob::dispatch($userIds, $newOptions);
Then create a job:
// app/Jobs/UpdateConsentsJob.php
public function handle()
{
foreach ($this->userIds as $userId) {
$user = User::find($userId);
$user->updateConsents($this->newOptions);
}
}
// config/app.php
'locale' => 'fr',
Then publish and override language files:
php artisan vendor:publish --tag="user-consent-lang"
Edit resources/lang/fr/user-consent.php.5.x with Filament 4.x or vice versa will break the package.composer.json:
"require": {
"filament/filament": "^5.0",
"visualbuilder/user-consent": "^5.0"
}
hasConsents() MethodUser model lacks the hasConsents() method, consent checks fail.User model:
use VisualBuilder\UserConsent\Concerns\HasConsents;
class User extends Authenticatable
{
use HasConsents;
}
failed_jobs table for queue failures..env mail settings (e.g., MAIL_MAILER=smtp).Mail::to($user)->send(new \App\Mail\TestMail());
HandlesUserConsent is used in the Registration class.consent_options config key exists and is an array.consents table.php artisan
How can I help you explore Laravel packages today?