Back

Where do you put your Traits as a Laravel developer? 🤔

#php #laravel
image

3

500

  • No matching results...
  • Searching...

/ 1000

I put them in their module and use the word Trait in the name to make my code easy to walk through for any PHP developer.

116

It depends. 👀
If the traits are more generic and used across the app, `/app/Traits` makes sense. For model-specific behavior `/app/Models/Traits`. As for naming, 'Concerns' can highlight intent better, but 'Traits' is simpler — and ultimately, I think it's down to preference and convention.

1

158

For traits that are designed for generic use, like they can be use in most of the classes, I prefer to put it on App/Traits. But if the trait can only be use for a very specific classes in a very particular way, I usually put them beside those classes directory, inside the Concerns. So for models, I do App/Models/Concerns

1

118