Trait is an implementation method to facilitate code reuse in PHP5.4, but few OF the PHP projects I have seen so far are actively used by programmers. There are many uses of trait in Laravel. For more information on how traits are used in Laravel, see where laravel uses traits.

I once shared an architectural practice of someone else in Laravel’s Object-oriented Architecture Reference for large projects that made extensive use of dependency injection. This is the way to go, and the same use of traits can do the same thing, but in many more scenarios.

The model used in the

As the project goes on, the model will gradually become large and difficult to maintain, because the traditional way is to write the database access logic in this, while in Laravel’s model, in addition to the data access logic, there will also be the association between data tables, accessors, modifiers, monitored fields, whitelists, blacklists and so on.

When there are so many things in a model, it is difficult to maintain the model. Of course, data access can be separated by Repository, but using Repository is a laborious and time-consuming transformation project when the project already has a lot of data access written in the model.

Traits allow you to separate your code without doing anything at the logical level.

We simply create a trait, move all the data access methods into it, and then use the trait in the original model to easily separate the code.

Similarly, you can separate accessors, modifiers, and even commonly used model definitions, such as Laravel’s soft delete, by defining code for common model functions, such as content auditing.

My advice is to put Model in the Models folder and the rest of traits in the folder created in Models so that changes can be easily found nearby. For example, I put the accessor modifier in the App /Models/Attribuite folder and then use the camel name of the model name and type name. Such as the User model of access modifier my file path is app/Models/Attribuite UserAttribute. PHP.

Of course, you can go further, defining accessors as separate traits and modifiers as separate traits.

app/Models/Attribuite/UserGetAttribute.php app/Models/Attribuite/UserSetAttribute.php

Use in Controller

In Laravel, there is a base class controller that has been used in several traits, such as authentication and validator. When we use base class controller, if there are fewer base class methods, we can write them directly in the base class controller. When there are many base class methods, it is also a hassle to maintain and find them. This is when traits can be used to classify them.

While some of the reusable code may only be used in a few controllers, you can use defined traits only in those controllers.

Another way to use is to separate the controller. When there are more and more methods in the controller, it is also necessary to separate the methods in the controller. The general approach is to create a new controller, and then move some methods in the original controller, and then modify the routing configuration. A trait enables separation without modifying the routing configuration. Create a new trait to move the method that requires separation over, and then use the trait in the original controller.