DEV Community

Cover image for Thin Controllers Fat Models
Code Of Accuracy
Code Of Accuracy

Posted on

Thin Controllers Fat Models

"Thin Controllers Fat Models" is an approach to organizing code in Laravel, a popular PHP framework. The basic idea behind this approach is to keep the logic of your application in your models, while keeping your controllers simple and focused on handling HTTP requests and responses.

In this approach, models are responsible for handling database queries, processing data, and implementing business logic. Controllers, on the other hand, are responsible for handling requests from the client, and returning responses. They should be kept as simple as possible, and should delegate most of their work to the models.

The benefits of this approach are numerous. By keeping the logic of your application in your models, you can easily test and reuse code, and make your application more modular. Your controllers become simpler and easier to maintain, and your code becomes more organized and easier to understand.

To implement this approach in Laravel, you can create a separate class for each model, and use it to encapsulate all the logic related to that model. You can also use Laravel's built-in Eloquent ORM to create relationships between models, and to perform database queries.

Overall, the "Thin Controllers Fat Models" approach is a great way to write cleaner, more maintainable code in Laravel. By separating concerns and keeping your controllers and models focused on their respective responsibilities, you can create a more scalable and robust application.

What I do like, though, from Taylor's opinion, is to have custom methods in Models. For a long time, there was a debate in community, that Eloquent Models need to have only Eloquent stuff - settings like $table or $fillables, methods for Relationships, Accessors/Mutators, and nothing more.
But if the creator of the framework himself says that you can put whatever you want there, as long as it doesn't include all the logic, I agree it's more readable than calling Service/Job directly from Controller.

What do you think? Agree with Taylor? How "thin" are your Controllers and Models?

Top comments (0)