So now I'm rewriting it, while trying to make it follow some patterns and also follow some guidelines for a better and cleaner code, for improved readability and maintenance later on.
With all that said, I spent this week reading a lot about the use of Service and Repository Patterns in Laravel, and I started doing it using both but now I get why some people said that it's over-engineering because for like 85% of the Models in the old project (there are more than 150 models), the respective repositories class will only have basic Eloquent methods. The repository will have method create() that has one line that is just calling the same model with $model->create(). So for a good chunk of the project the repositories will be kinda useless.
The problem is the other 15% of the Models and data in general... a lot of the pages in our system shows statistics charts (line, pie, bar, polar, radar, etc) using ChartJS, and most of the queries for generating those charts are very complex and not using Eloquent, and just plain SQL as this is easier to write when you are dealing with a SQL with 80 lines or more, some even use database stored procedures and db functions calls.
Because of those queries, I wanted to go for the repository pattern but now I'm not so sure as there is so much redundancy for a good part of the code like I said before.
I spent some time searching, and for getting more inputs from other Laravel developers, I wanted to ask to you guys that work in complex projects, where do you store very complex queries? Specially those that are not even using Eloquent methods to be generated?
I saw some people leaving those complex, DB raw plain SQL queries at the Controller itself, others on a Service class, some people left them directly inside the Models as a method to be called like $user->getComplexChartData() after using a User::find($id), some create an Utils class, other guy made a class called UserCharts class inside a new directory called Charts...
The thing is, none of the solutions I saw looked like the "perfect match" for me.
How do you guys handle those?
edit: just adding that the queries result have to obviosuly be manipulated by the PHP for adding/treating some data, so that's why its planned to be on a method for each submitted by /u/Cthulhu-Cultist
[link] [comments]
0 comments:
Post a Comment
Thanks