Every web developer has its own style when it comes to writing codes. It’s the same if we’re working using the Laravel framework where everything is in place but usually we misused the terminology here. It’s fine when it comes to different styles, but we must make sure that our codes follow the good style. This means that our codes have to be scalable, maintainable, and testable.
What makes our codes become a bad or good one? Well, because PHP is an OOP language, then we should follow the OOP principle such as the SOLID principle and consider using the OOP mechanism such as inheritance, abstract, and so on. Also, Laravel has a big community that sometimes there are some conventions that the community created. So other Laravel developers that also follow the convention can easily understand our codes better and faster. In this article, I’ll show you 7 best practices in Laravel based on the OOP principle & some Laravel community conventions.
Fat Models, Skinny Controllers
If we have a pretty complicated query builder or raw SQL, we should move this query into Models or Repositories.
Business Logic in Service Class
Related to the 1st point above where we should have a skinny controller, then we should move all the business logic in the separate service classes. So controllers should only have one responsibility, and hopefully, we can reuse this service in other controllers.