公告: 旧版文档请移步 -> http://v2.laravel-filament.cn/docs

Languages

Version

Theme

Admin Panel

Users

By default, all App\Models\Users can access Filament locally. To allow them to access Filament in production, you must take a few extra steps to ensure that only the correct users have access to the admin panel.

Authorizing access to the admin panel

To set up your App\Models\User to access Filament in non-local environments, you must implement the FilamentUser contract:

<?php
 
namespace App\Models;
 
use Filament\Models\Contracts\FilamentUser;
use Illuminate\Foundation\Auth\User as Authenticatable;
 
class User extends Authenticatable implements FilamentUser
{
// ...
 
public function canAccessFilament(): bool
{
return str_ends_with($this->email, '@yourdomain.com') && $this->hasVerifiedEmail();
}
}

The canAccessFilament() method returns true or false depending on whether the user is allowed to access Filament. In this example, we check if the user's email ends with @yourdomain.com and if they have verified their email address.

Setting up avatars

Out of the box, Filament uses ui-avatars.com to generate avatars based on a user's name. To provide your own avatar URLs, you can implement the HasAvatar contract:

<?php
 
namespace App\Models;
 
use Filament\Models\Contracts\FilamentUser;
use Filament\Models\Contracts\HasAvatar;
use Illuminate\Foundation\Auth\User as Authenticatable;
 
class User extends Authenticatable implements FilamentUser, HasAvatar
{
// ...
 
public function getFilamentAvatarUrl(): ?string
{
return $this->avatar_url;
}
}

The getFilamentAvatarUrl() method is used to retrieve the avatar of the current user. If null is returned from this method, Filament will fall back to ui-avatars.com.

Configuring the name attribute

By default, Filament will use the name attribute of the user to display their name in the admin panel. To change this, you can implement the HasName contract:

<?php
 
namespace App\Models;
 
use Filament\Models\Contracts\FilamentUser;
use Filament\Models\Contracts\HasName;
use Illuminate\Foundation\Auth\User as Authenticatable;
 
class User extends Authenticatable implements FilamentUser, HasName
{
// ...
 
public function getFilamentName(): string
{
return "{$this->first_name} {$this->last_name}";
}
}

The getFilamentName() method is used to retrieve the name of the current user.

Edit on GitHub

Still need help? Join our Discord community or open a GitHub discussion