Laravel Eloquent Report: Group By Year with Raw Queries

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys in this video I will show you an interesting quick example of grouping the data in eloquent for something like this so I received this email so I didn't invent the situation so for example if you have these three tables and this is the result that you want which is basically grouping by calculated condition which means you need to group the transactions by car which is okay car has a name but also you need to group that by the year of created ad like this so from that date you should extract Year and that would be second grouping parameter so it's not just grouped by two Fields but one field plus calculated year of the second field how do we do that in laravel I've recreated that project so in the database we have something like this so transactions with created add then cars and then categories and this is the query so this is a query Builder query so we're building the query and then we select raw data because we need to have year calculated here and that's why that all becomes raw string and then if we assign that calculated value to Alias of transaction year as well as a few more aliases here we can use that Alias as in group by raw this is the essential thing in this video so the result of that visually is this table or a list in fact so in the blade if we click that blade in transactions for each Loop those aliases here category name and transaction year also some formatting for the number but this part is pretty simple the most complicated part in here in this code is probably that group by raw and a few more joins but they are not complex at all also one more thing to notice is query thing but I'm using that only for readability quite often because if I don't use that query the select row will be on the same line and this is much less readable you would probably agree so let's get back to that and I have a separate video about that query thing and I will link that in the description below but basically what I wanted to show you in this video is this group by raw so you can perform any query of your MySQL or your database engine then assign the Alias and you can use that alias in group by row below what do you think would you do it the same way or you have other suggestions let's discuss in the comments below and see you guys in other videos
Info
Channel: Laravel Daily
Views: 6,575
Rating: undefined out of 5
Keywords:
Id: Nmfaus8n0TU
Channel Id: undefined
Length: 2min 29sec (149 seconds)
Published: Fri Jun 02 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.