sindyko/aliaser
Laravel package for elegant alias management across Eloquent models, Livewire forms, DTOs, collections, and enums. Use short aliases and the Entity facade (Entity::user(1)), auto-sync morph maps, and optimize Livewire snapshots with custom synthesizers.
Installation:
composer require sindyko/aliaser
php artisan aliaser:install
This publishes the config file and creates default migrations for morph map sync.
Register Aliases (in AppServiceProvider::boot()):
use App\Models\User;
use Sindyko\Aliaser\Facades\Entity;
modelsMap(['user' => User::class]);
First Use Case:
Replace User::find(1) with Entity::user(1) in your controllers/views.
app/Facades/Entity.php (or Sindyko\Aliaser\Facades\Entity)php artisan aliaser:help for quick referenceconfig/aliaser.php for morph map and overwrite settings// Query Builder
$users = Entity::user()->where('active', true)->get();
// Static Methods
$count = Entity::user()->count();
$newUser = Entity::user()->create(['name' => 'Alice']);
// Finders
$user = Entity::user(1); // Single
$users = Entity::user([1, 2, 3]); // Multiple
$user = Entity::user(1, ['id', 'name']); // With columns
// Component property
public User $user; // Automatically serialized as 'user' alias
// Form handling
public PostForm $form; // Serialized as 'postForm'
// Migration (auto-handled by Aliaser)
$table->morphs('commentable'); // Stores 'post' instead of 'App\Models\Post'
// Usage
$comment->commentable_type; // Returns 'post'
// DTO in Livewire
public UserFilterDTO $filters; // Serialized as 'userFilter'
// Eloquent Collection
public Collection $posts; // Serialized as 'elqn_clctn' with model alias
// app/Providers/AppServiceProvider.php
public function boot(): void
{
// Group related models
modelsMap([
'user' => User::class,
'admin' => Admin::class,
'client' => Client::class,
]);
// Livewire forms
formsMap([
'userForm' => UserForm::class,
'postForm' => PostForm::class,
]);
// DTOs/Value Objects
objectsMap([
'money' => Money::class,
'userFilter' => UserFilterDTO::class,
]);
}
// Dynamic registration in a service
ModelRegistry::register('tempUser', TempUser::class);
// Clear after use
ModelRegistry::forget('tempUser');
// Clear caches before tests
ModelProxy::clearStaticMethodsCache();
ModelRegistry::clear();
// Reset morph map
Relation::enforceMorphMap([]); // Clear all
# List all aliases
php artisan aliaser:list --json | jq
# Check if an alias exists
php artisan aliaser:list --models | grep 'user'
# Get help for a specific registry
php artisan aliaser:help forms
Relation::enforceMorphMap() elsewhere, aliases may not sync.use_morph_map in config if needed, or ensure Aliaser runs last in boot().'allow_overwrite' => true in config (not recommended for production).protected properties with fillable or guarded arrays.ModelProxy::clearStaticMethodsCache() before tests.php artisan aliaser:list --json
Check for typos or missing registrations.
// Dump current morph map
dd(Relation::getMorphMap());
// Force sync
Relation::enforceMorphMap(ModelRegistry::getMorphMap());
--debug flag:
php artisan serve --debug
ModelProxy::clearStaticMethodsCache() to measure Reflection overhead.AppServiceProvider).// Extend AbstractAliasRegistry
class CustomRegistry extends AbstractAliasRegistry
{
protected $key = 'custom';
protected $aliasPrefix = 'custom_';
}
// Register globally
app()->singleton('customRegistry', fn() => new CustomRegistry());
// Extend AbstractSynthesizer
class CustomSynth extends AbstractSynthesizer
{
public function canHandle($value): bool
{
return $value instanceof CustomClass;
}
public function synthesize($value)
{
return ['custom', $value->toArray()];
}
}
// Register with Livewire
Livewire::synthesize('custom', CustomSynth::class);
// Hook into registration
ModelRegistry::register('dynamic_'.Str::uuid(), DynamicModel::class);
// Register only in specific environments
if (app()->environment('local')) {
modelsMap(['debugUser' => DebugUser::class]);
}
user-post) for nested aliases.php artisan aliaser:list + IDE autocomplete.Entity facade in tests:
$this->app->instance('aliaser', fn() => new MockEntity());
wire:model.live for reactive forms with aliases.How can I help you explore Laravel packages today?