Filament Register: Automatically Assign Role from Spatie Permission

Sometimes, after registration, you want to perform some operation with the new User object, like assigning a default Role.

In what Filament place you need to "hook" that $user->assignRole() code?

In this example, I will use a spatie/laravel-permission package, with the goal to automatically attach a role for the new user.

The official docs of the package show this code:


In general, how to perform any operation on the new $user object, after the registration? In my case it's about the role, but the same logic applies to any other methods, like attaching more relationship data, for example:


But the question is where to put this code in Filament? And how to get that $user?

Option 1. Laravel Model/Observer.

If you want to assign that role to all the new Users, even if they are registered outside of Filament, then you should do it on Laravel level, in the Model.

class User extends Authenticatable implements FilamentUser
// ...
protected static function booted(): void
static::created(function (User $user) {
// ...

You can also do the same in the Eloquent Observer.

Option 2. Filament Register Class: handleRegistration()

But if you want to add this new behavior specifically for Filament forms, there's a new way that was made even better in Filament v3.2.65.

First, you need to create a new Registration class that would extend the internal Filament Register class.

In Laravel 11, you can do it with make:class Artisan command:

php artisan make:class Filament/Auth/CustomRegister

Then, inside of that class, you override one method, adding one line:


use Filament\Pages\Auth\Register;
class CustomRegister extends Register
protected function handleRegistration(array $data): Model
return $this->getUserModel()::create($data);
$user = $this->getUserModel()::create($data);
return $user;

Finally, you register that custom class in your PanelProvider.


// ...
use App\Filament\Auth\CustomRegister;
class AdminPanelProvider extends PanelProvider
public function panel(Panel $panel): Panel
return $panel
// ... other functions

That's it!

After the registration, the role should be assigned successfully. Here's my screenshot from the DB table model_has_roles:

If you want more Filament examples, you can find more real-life projects on our

No comments or questions yet...

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 60 courses (1084 lessons, total 42 h 44 min)
  • 80 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent Premium Tutorials