Тёмный

How/When to Use Traits in Laravel: My Opinion 

Laravel Daily
Подписаться 144 тыс.
Просмотров 26 тыс.
50% 1

Опубликовано:

 

10 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 48   
@astrofiapp3059
@astrofiapp3059 2 года назад
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
@JouvaMoufette
@JouvaMoufette 2 года назад
Exactly this. Literally this. Traits shouldn't be used to put code in another location to make your code look cleaner; it should be used to extend a class after you've already extended it via inheritance. They're not a replacement for jobs or service classes.
@TheMarcelitto
@TheMarcelitto 2 года назад
traits are the way that php implements multiple inheritance which is not implemented, so you are absolutely right Povilas.
@SheikhHeera
@SheikhHeera 2 года назад
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.
@KlethonioFerreira
@KlethonioFerreira 3 месяца назад
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.
@cooop92
@cooop92 2 года назад
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.
@phoenixedge4049
@phoenixedge4049 2 года назад
Agree completely. Traits are for code to be reused across different classes.
@alexaverkiyev9099
@alexaverkiyev9099 2 года назад
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.
@UnknownUser-ud1es
@UnknownUser-ud1es 2 года назад
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.
@shahsawoodshinwari
@shahsawoodshinwari 8 месяцев назад
Definitely, when refactoring code, so re-usable is refactored into traits, which adds additional power to classes.
@ndeblauw
@ndeblauw 2 года назад
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)
@JovenAlbarida
@JovenAlbarida 2 года назад
your opinion works for me
@mkyg8871
@mkyg8871 2 года назад
I agree with your opinion 100%.
@EduardR10
@EduardR10 2 года назад
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. :)
@stojankukrika7242
@stojankukrika7242 2 года назад
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 :)
@techfuture-code-tv
@techfuture-code-tv 2 года назад
Now I understand better with use case of trait
@ryiad2010
@ryiad2010 29 дней назад
very useful thank you
@biohazard1964
@biohazard1964 2 года назад
extremely useful. thanks for your videos.
@L-A-O-S
@L-A-O-S 2 года назад
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.
@codelaravel
@codelaravel 2 года назад
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.
@LaravelDaily
@LaravelDaily 2 года назад
You can. But helpers are not within OOP structure, so unless they are global helpers for everything (like, date formatting or something), they go against OOP principles.
@alexaverkiyev9099
@alexaverkiyev9099 2 года назад
because you can't test your code with helpers
@Snoopal1
@Snoopal1 2 года назад
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.
@SanderCokart
@SanderCokart 2 года назад
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?
@rezajabbari361
@rezajabbari361 2 года назад
Perfect!
@GergelyCsermely
@GergelyCsermely 2 года назад
Thanks
@RehmanKhan-zv2rl
@RehmanKhan-zv2rl 2 года назад
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.
@sufyant21
@sufyant21 2 года назад
rarely use traits, prefer use helpers instead
@ROX2
@ROX2 2 года назад
Fine
@SalomDunyoIT
@SalomDunyoIT 2 года назад
agree
@IlPandax
@IlPandax 2 года назад
I would have used traits only if there were multiple types of order. If you have one single order type, using a trait is overengeneering. IMHO of course.
@SanderCokart
@SanderCokart 2 года назад
Any knowlegde on Polymorphic Relationship event listeners? I have a Post and when its Polymorphic Status relationship updates via Post::find(1)->status()->sync(1) I wish to fire an event that sets the published_at if null and switches all the URL's in my markdown from private domain/files/id version to the public/uploads/filename.jpg version. It also seems that on relationships you cannot get dirty relationships correct? Or at least you cannot make relationships without saving to the database with polymorphic similair to fill() or make(). Any personal experiance with such a thing? if not ill just have to compare everything and fire stuff manually from the controller. (adding bulk, and sure bit more readable for a junior dev joining in (won't happen)) but anyway.
@free2idol1
@free2idol1 Год назад
4:58 but why it needs to have `HasMedia` Interface implemented before using `InteractsWithMedia` trait?
@LaravelDaily
@LaravelDaily Год назад
Interface is a set of rules. Trait adds one way to implement those rules.
@free2idol1
@free2idol1 Год назад
@@LaravelDaily thanks but given the context of that package, does that still make sense if InteractsWithMedia` trait is used without the interface? and in which scenario, we should use Interface along side with Trait?
@SumitSingh1356
@SumitSingh1356 2 года назад
Thanks for this video! It solved me a problem. I have a quick question, i am planning to create a new website from scratch of an existing one which is based on opencart. So what do you recommend, should i use the same tables as how opencart uses or migrate all the data totally to a new schema?
@LaravelDaily
@LaravelDaily 2 года назад
I haven't used opencart so can't comment. Maybe you should use opencart for new website, too, instead of reinventing the wheel?
@tbugaevsky
@tbugaevsky 2 года назад
Agree that traits are not the replacement of services.
@zkabadu
@zkabadu 2 года назад
Traits are kind of polluting your code and shoting your own leg. There might be some cases where traits might be useful but in most cases they make stuff less readable, indirect and hard to test. My suggestion: when you're not sure if you should use a service class or a trait: use a service class.
@SunilYadav-tm6kt
@SunilYadav-tm6kt 2 года назад
Is repository pattern over-engineering in laravel ?
@LaravelDaily
@LaravelDaily 2 года назад
Yes, in my opinion
@SunilYadav-tm6kt
@SunilYadav-tm6kt 2 года назад
@@LaravelDaily Thanks
@JohnnyBigodes
@JohnnyBigodes 2 года назад
When you are trying to hard to come up with a trait, just use a service class. I have been bitten by traits in the past, but this is just my opinion.
Далее
Faster Eloquent: Avoid Accessors with Foreach
9:35
Просмотров 53 тыс.
Laravel Junior Code Review: 12 Tips on Everything
15:30
Laravel Service Providers: All You Need to Know
13:13
OOP PHP | What is Traits and Why we need it #20
9:26
9 Tips for Shorter Laravel Code
10:16
Просмотров 61 тыс.
The Tools I Use to Build Products in Laravel
19:00
Просмотров 27 тыс.