How/When to Use Traits in Laravel: My Opinion

preview_player
Показать описание
In a recent video, I've transformed Controller code into a Service class, and some people in the comments argued about using Traits instead. Let me comment on that example, adding a few more examples of Traits in other projects.

- - - - -
Support the channel by checking out our products:
Рекомендации по теме
Комментарии
Автор

Well explained, in my experience I completely agree with you. So many times I’ve seen traits used badly and when it comes to maintaining code it can be a nightmare. The hardest part is always deciding which class (model/service/trait) should be responsible for some functionality, that just comes with experience. Go with what makes most sense at the time and which option you find easiest to justify to your peers.

cooop
Автор

Traits are PHP way of overcoming the inability to do multi-inheritance in a class so when you use a trait in a class you need to think of it as inheriting a super-class which is the trait in this case

astrofiapp
Автор

It's not about moving out the code, instead, it's about moving out (Separation) of logic. Keeping this in mind, use traits for moving some repeatable code and use some other classes (model or service, action, job whatever you say) to move/separate the logic.

SheikhHeera
Автор

traits are the way that php implements multiple inheritance which is not implemented, so you are absolutely right Povilas.

TheMarcelitto
Автор

To me traits should act like small 'extensions' of a class, which will have a pretty high cohesion with the class that uses it.
Laravel traits that are used in BaseController act more like helpers, that ease the usage of dispatching jobs, authorizing users, and so on. The problem is, we all have controllers that don't use none of these traits, but still load them. Solution - either make different BaseControllers, or use these traits in each of your controller.
Spatie trait on the other hand, extends the functionality of particular class, whose logic rules are defined with an interface HasMedia. That makes huge difference with Laravel's helper traits.
To me Laravel's method of using traits is a bit of a smell, I'd rather make different base controllers, or use trait in the particular controller, or even inject service as a dependency. The last option lets test your code much easier.

alexaverkiyev
Автор

Agree completely. Traits are for code to be reused across different classes.

phoenixedge
Автор

How I learned when to use trait in case of controller we are using and was easy to understand if example of HasCRUD trait .
This trait has methods of resource controller and can be used in system which heavily rely on CRUD to make development task easier. As CRUD was concept well understood even among beginner this example helped me a lot.

UnknownUser-udes
Автор

Hi Povilas, I fully agree with you from a "coding theory" point of view. But on the other hand, personally I'm using Traits to add "view presenter" functionality to some of my models, which is kind of an abuse of the trait, but it is soo much cleaner and handier in the implementation. So I guess my final opinion would be to be a bit lenient on the use of traits. Main thing to keep in mind is to be consequent in your codebase. Don't use an Action once and a Trait in another case. But always refactor to traits, or always refactor to Actions. And additionally, stick to a clear naming, e.g. name your trait "OrderInvoiceAction" to make it more clear what kind of functionality it contains (or put it in a separate namespace like Traits/Actions)

ndeblauw
Автор

I'm sorry but these people need to review the concept of MVC. Good content! You're completely right and these are simple architectural concepts.

KlethonioFerreira
Автор

Hello teacher, the times I have used a trait it has been to add functionalities that can be used from various classes, for example, that "pushStatus" method, I would probably have it in a trait.
I've even scoped traits, so filtering by status or things like that are features I can add to eloquent, just by using the trait and not declaring a scope for each model that requires it. I have used a translator, I hope everything is understood. :)

EduardR
Автор

I used Traits on one of my microservice, but figure out something. If you add more than one trait in some cases there were some conflicts happening there. So refactor all traits into services and other things and after that, all work much better and without any conflicts. So Traits can be Ok but too many traits didn't good at all :)

stojankukrika
Автор

Definitely, when refactoring code, so re-usable is refactored into traits, which adds additional power to classes.

shahsawoodshinwari
Автор

It a holy wars speaking about traits. I met people who say that this is a real evil, some one says trait is awesome. I have a positive attitude to everything, I don’t see anything bad in traits, but I understand that they are like a library to a class.

L-A-O-S
Автор

Traits should, by design, be something reusable. E.g. if you use uuid instead of incrementing integers for Ids in you mordel. Then you can inherit the functionality into the models which should use that format for the identifier.

Snoopal
Автор

Hello Povilas, I agreed but why we can create helpers in laravel. Helpers can short code as well. we directly call helper function instead of importing trait in the model. why we should not use helper instead of trait.

codelaravel
Автор

I think better way to use trait is that, it should be used where same code is repeated in different controller, it something that I used to follow concept like DRY.

RehmanKhan-zvrl
Автор

Now I understand better with use case of trait

techfuture-code-tv
Автор

is it possible to update polymorphic relationships and update the model in a single query with eloquent or would you need to use DB:Raw for this?

SanderCokart
Автор

extremely useful. thanks for your videos.

biohazard
Автор

4:58 but why it needs to have `HasMedia` Interface implemented before using `InteractsWithMedia` trait?

freeidol
welcome to shbcf.ru