kkszymanowski/traitor
Traitor brings traits to Eloquent like first-class relations. Define reusable, composable model fragments and access them via $model->traitName, eager load with with('traitName'), and query through them—keeping models slim while sharing behavior and attributes.
Install via Composer: composer require kkszymanowski/traitor. Once installed, import the Traitor facade or inject Traitor\TraitResolver directly. The most common first use case is inspecting all traits used by a class—including those brought in via nested traits—e.g., Traitor::forClass(MyService::class)->getAllTraits(). This replaces fragile regex searches or manual class_uses_recursive() calls, especially useful when auditing services for domain-specific traits (e.g., SoftDeletes, Timestamps, or custom domain traits like 可发布()). Start by running a quick sanity check in Tinker: Traitor::forClass(App\Models\User::class)->getAllTraitNames().
php artisan audit:traits) to flag classes missing expected traits or violating architectural rules (e.g., “no service may use Eventable trait without also using Validatable”).$this->assertTrue(Traitor::forClass($subject)->hasTrait(WithPermissions::class)).if (Traitor::forClass($model)->hasTrait(Translatable::class)) { ... }).AuditLoggable) aren’t accidentally removed during refactors.getAllTraits() returns all traits from the hierarchy (including inherited traits), but getDirectTraits() returns only those used directly in the class—ensure you choose the right method based on whether inheritance matters.use or \Traitor\... aliases to avoid ambiguity.Trait A uses Trait B and Trait B uses Trait A—as this is undefined behavior in PHP and may cause silent failures or crashes.TraitResolver instances per class in long-running processes (e.g., CLI commands or queue workers). Use Traitor::forClassCached() if available in later versions.Traitor::forClass(...), ensure the facade is registered in config/app.php under 'Traitor' => Traitor\Facades\Traitor::class. Misregistration is a common first-time pitfall.How can I help you explore Laravel packages today?