@ddsameera The Repository pattern is by far the worst offender I see in Laravel applications. It’s never implemented properly, people use it to avoid using Eloquent in controllers but guess what? They just try and re-implement Eloquent models in repositories any way. But Eloquent is a massive library, so these repositories are just full of repetitive methods that never live up to the usefulness of Eloquent itself.
For example, developer wants to fetch posts in their application, so they create a PostRepository with an all method. Cool. But now they realise they need paginated posts, so they add a getPaginatedList method. But then they realise the admin panel should list all posts, whereas the front-end should only list published posts, so end up with a getPaginatedList method and a getPublishedPaginatedList. And the repository then just gets stuffed with more and more edge cases as they crop up: the need to filter, the need to sort, eager-load relations and counts (say number of comments on posts), and so on.
So yeah, avoid the Repository pattern. They just introduce more problems than they apparently solve.
Personally, I just use Eloquent in my controllers. It just works. I’ve worked on dozens and dozens of Laravel projects for around 8 years now, and it’s worked fine in projects of all sizes, from small sites to large ones. If you find there’s logic you need to use in multiple places then by all means extract that logic to a dedicated service class or action class.
However, it is worth to think what if the client proposes to change the data structure and instead of in MySQL/Postgresql from now on we are going to keep the data somewhere else, in the data engine which is not supported by Eloquent? When we write such a code, such a change may turn out to be very difficult to implement, or even impossible!
This is the most annoying straw man argument ever for repositories and boils my piss. “But what if we change our database?!” In nearly 15 years of programming, not once have I worked on a project where as a team we’ve decided, “You know what? Let’s change our database.” And not once have I worked on a Laravel project and gone, “You know what? Let’s use a database that isn’t supported by Eloquent.”
If you’re using Laravel, then there’s 99.9% change you’re going to be using Eloquent and a supported database.