DEV Community

Cover image for Customizing Filament User Model
yebor974 for Filament Mastery

Posted on • Originally published at filamentmastery.com on

Customizing Filament User Model

Filament offers significant flexibility for tailoring the User model to meet the specific needs of your application. Below are some commonly used configurations.

Managing Avatar with the HasAvatar Trait

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;

    // ...
}

Enter fullscreen mode Exit fullscreen mode

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;
}

Enter fullscreen mode Exit fullscreen mode

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;
        },
    );
}

Enter fullscreen mode Exit fullscreen mode

This code will delete previous avatar file for the updated user.

Managing Name with the HasName Trait

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;

    // ...
}

Enter fullscreen mode Exit fullscreen mode

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;
}

Enter fullscreen mode Exit fullscreen mode

Controlling Access with the FilamentUser Interface

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');
    }
}

Enter fullscreen mode Exit fullscreen mode

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.

📬 Join the community on filamentmastery.com — it's free!

Top comments (0)