ยท yebor974 ยท Getting Started, Tutorials
Customizing Filament User Model
Learn how to customize the Filament User model with traits like HasAvatar and HasName, control panel access with FilamentUser, and configure multi-tenancy.

Filament offers significant flexibility for tailoring the User
model to meet the specific needs of your application. Below are some commonly used configurations.
HasAvatar
Trait
Managing Avatar with the To associate an avatar with each user, you can use the HasAvatar
trait. This trait simplifies avatar management by providing ready-to-use methods.
use Filament\Models\Concerns\HasAvatar;
class User extends Authenticatable
{
use HasAvatar;
// ...
}
The HasAvatar
trait assumes that your User
model has an avatar image path like avatar_url
attribute. You can customize the getFilamentAvatarUrl()
method to define the logic for retrieving the avatar URL:
public function getFilamentAvatarUrl(): ?string
{
return $this->avatar_url ? asset('storage/' . $this->avatar_url) : null;
}
A quick tip for removing an already registered avatar on a user is to define a setter on the avatar_url
attribute (you have to specify your storage path / disk):
protected function avatarUrl(): Attribute
{
return Attribute::make(
set: function (?string $value) {
if (!empty($this->avatar_url) && (is_null($value) || $value !== $this->avatar_url)) {
Storage::disk('public')->delete($this->avatar_url);
}
return $value;
},
);
}
This code will delete previous avatar file for the updated user.
HasName
Trait
Managing Name with the To handle user name, the HasName
trait can be used. It allows you to easily define and retrieve the user's full name.
use Filament\Models\Concerns\HasName;
class User extends Authenticatable
{
use HasName;
// ...
}
By default, Filament use the name
attribute. If your model uses different attributes, you can override the corresponding method to adapt the logic to your database structure:
public function getFilamentName(): string
{
return $this->username;
}
FilamentUser
Interface
Controlling Access with the To restrict access to Filament panels, implement the FilamentUser
interface in your User
model. This allows you to define specific rules to determine which users can access different sections of the admin panel.
use Filament\Models\Contracts\FilamentUser;
use Filament\Panel;
class User extends Authenticatable implements FilamentUser
{
// ...
public function canAccessPanel(Panel $panel): bool
{
// Logic to determine if the user can access the panel
return $this->hasRole('admin');
}
}
In this example, only users with the admin
role can access the Filament panel. Adjust the logic in the canAccessPanel()
method according to your application's needs.
Multi-Tenant Configuration
Multi-tenant management enables your application to serve multiple clients from a single instance, isolating each client's data. Filament provides tools to facilitate this configuration, but it's essential to understand the security and structural implications for your application.
For a detailed implementation of multi-tenant configuration in Filament, you can read this article.
This article provides step-by-step instructions to configure a multi-tenant application with Filament.
Conclusion
Customizing the User
model in Filament is crucial to tailoring your application to the specific needs of your users. By leveraging appropriate traits and interfaces, you can efficiently manage avatars, names, panel access, and more.
You can view some of these configurations directly in the Filament Mastery back office, such as modifying your avatar in your profile.