Installation:
composer require saade/filament-facehash
Run php artisan vendor:publish --provider="Saade\FilamentFacehash\FacehashServiceProvider" if customization is needed.
First Use Case:
Replace default Filament avatars in your admin panel by adding the provider and plugin to your Panel configuration:
public function panel(Panel $panel): Panel
{
return $panel
->defaultAvatarProvider(FacehashProvider::class)
->plugins([
FacehashPlugin::make(),
]);
}
This immediately replaces all user avatars in Filament with deterministic, SVG-based facehash avatars.
Where to Look First:
FacehashPlugin::make() for available methods.Basic Integration:
panel() as shown above. No additional setup is required for default behavior.Dynamic Configuration: Use the fluent API to customize avatars per panel or tenant:
FacehashPlugin::make()
->size(fn (User $user) => $user->isAdmin() ? 50 : 40) // Dynamic sizing
->variant(fn (User $user) => $user->prefersDarkMode() ? Variant::Solid : Variant::Gradient);
Model-Specific Avatars: Extend the trait for custom logic (e.g., override hash generation):
use Saade\FilamentFacehash\HasFacehashAvatar;
class User extends Authenticatable
{
use HasFacehashAvatar;
public function getFacehashAttributes(): array
{
return [
'name' => $this->full_name, // Custom attribute
'color' => $this->theme_color, // Dynamic color
];
}
}
Conditional Avatars: Disable facehash for specific users/models:
public function getFacehashAvatarUrl(): ?string
{
return $this->shouldUseFacehash ? parent::getFacehashAvatarUrl() : null;
}
Caching:
Leverage Filament’s built-in caching for avatars by implementing ShouldCacheAvatar:
use Saade\FilamentFacehash\Concerns\ShouldCacheAvatar;
class User extends Authenticatable
{
use ShouldCacheAvatar;
}
->colors() to match your app’s brand palette.Filament\Panel::defaultAvatarProvider() for hybrid setups:
->defaultAvatarProvider(function ($record) {
return $record->avatar_url ?: FacehashProvider::make($record);
});
Deterministic Hashes:
name="John") always produces the same avatar. Use unique attributes (e.g., email) if collisions are a concern:
->hashAttributes(['name', 'email'])
Initials Override:
->initial(false) hides letters entirely. Ensure name or other attributes are still provided for consistency.SVG Rendering:
Avatar component:
Avatar::make('user')
->avatar($user->facehash_avatar_url)
->width(40)
->height(40)
->circle();
Plugin Registration:
FacehashPlugin::make() to panel()->plugins() will silently ignore the provider.php artisan filament:cache-reset) or check for cached responses.name or other hash attributes are non-empty. Log the generated hash:
dd(Facehash::generate(['name' => $user->name]));
->size(200)) may impact performance. Test in the playground first.->colors() with an array of strings:
->colors(['#hex1', '#hex2']) // Not ['rgb(0,0,0)', ...]
Variant::Gradient and Variant::Solid are supported. Custom variants require extending the facehash package.Custom Hash Generation:
Override getFacehashAttributes() in your model to modify input data:
public function getFacehashAttributes(): array
{
return [
'name' => strtolower($this->name),
'seed' => $this->created_at->timestamp, // Add entropy
];
}
Plugin Events: Listen for avatar generation via Filament’s event system:
FacehashPlugin::make()
->listeningForAvatarEvents();
// Then handle in EventServiceProvider:
protected $listen = [
'saade.facehash.generating' => [YourListener::class],
];
Multi-Panel Support: Register different configurations per panel:
// admin-panel.php
FacehashPlugin::make()->variant(Variant::Solid);
// customer-panel.php
FacehashPlugin::make()->variant(Variant::Gradient);
How can I help you explore Laravel packages today?