traits

Encapsulate shared Eloquent model behavior in PHP traits.

3|Updated Feb 13, 2026
One-click install
npx skills add https://github.com/codebar-ag/coding-guidelines --skill traits-codebar-ag
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: traits
Source: https://github.com/codebar-ag/coding-guidelines/tree/main/resources/boost/skills/traits
Command: npx skills add https://github.com/codebar-ag/coding-guidelines --skill traits-codebar-ag

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Reusable behaviour across multiple unrelated classes is hard to maintain when code is duplicated in each model. Traits provide a centralized place to implement common functionality such as Eloquent scopes, accessors, lifecycle hooks, and stateless helpers.

Core Features & Use Cases

  • Single-responsibility blocks: Encapsulate common behaviour in small, well-scoped traits that can be reused by multiple models.
  • Eloquent integration: Provide reusable scopes, accessors, and model hooks that can be shared across different models.
  • Maintainability: Reduce duplication and improve consistency across the codebase by applying traits where behaviour is shared.

Quick Start

Create a trait in App\Traits to share common behaviour and apply it to multiple models.

Frequently Asked Questions about traits

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I reuse Eloquent scopes and accessors across multiple Laravel models?▼

You can reuse Eloquent scopes and accessors by encapsulating them in PHP traits. Define the shared behaviour in a trait and apply it to multiple models to centralize logic and reduce code duplication.

What is the best way to avoid duplicating lifecycle hooks in Laravel Eloquent models?▼

The best way to avoid duplicating lifecycle hooks is to extract them into single-responsibility PHP traits. This ensures consistent behavior across different models while maintaining a centralized and maintainable codebase.

How do I structure PHP traits for Laravel models to ensure safe code reuse?▼

Structure PHP traits by following single-responsibility guidelines and established naming conventions. Encapsulate specific lifecycle hooks, accessors, or scopes in small, well-scoped blocks to ensure safe and consistent reuse.

When should I use traits instead of duplicating behavior in individual Eloquent models?▼

Use traits when you need to share common behavior across multiple unrelated Eloquent models. If you find yourself duplicating scopes, accessors, or lifecycle hooks, extracting them into a trait improves maintainability and consistency.

Can I share stateless helpers across different Laravel models using PHP traits?▼

Yes, you can share stateless helpers across different Laravel models using PHP traits. Traits provide a centralized place to implement common functionality, reducing duplication and improving consistency throughout your application.

Does using traits for shared behavior affect model lifecycle integration in Laravel?▼

Using traits for shared behavior integrates safely with model lifecycles when following defined lifecycle integration rules. This ensures that hooks and scopes applied through traits execute consistently without causing conflicts.