Man this is vital, since fundamentally what we are trying to accomplish is how can we have the report and also be somewhat flexible (since in general clients may want/ask for other bits of data). Recently in a project I've been working, on stumbled across a similar situation for getting the overall meetings for a user and also getting a list of users which you have had a meetings with to not duplicate. Initial approach was using joins, distinct, group by and aggregate collection. After a few refactors ended up coming to the conclusion that at least on the project I was working on the use of sub-queries to accomplish these extra bits of info (count of meetings and list of users you've had meetings with) seemed like a much more flexible approach so in the months ahead wouldn't have to deal with headaches. After checking out your implementation, feel like the potential refactor to using sub queries, could make your logic from lines 24 to 34 lets rigid. Regardless you are already accomplishing a single sql query.
Starting with the end in mind is such a cliche thing to say these days and yet I still find myself doing the complete opposite. You remind me to start with the blade file. Find out what I am looking for in the end and work back from there. The code is always much more readable and quicker to work out. I am often building overly engineered monstrsities when I don't take a moment to think about where do I want to ultimatly go.
You did it well, but scope can be a batter way re-using if needed. Records (rows) can also be presented as COLUMNS at DB level instead of iterating collection. No offense, just suggestions.
I have a question: is there a difference between using $cards->each and foreach($cards as $card), and what is it (performance, readability, something else)? Thanks.
For this example, it was a report that doesn't require pagination, but if you do require it then you can create your own paginator or customize the default Laravel one, just read the Laravel documentation.
Can you please tell me, is there some resource to learn eloquent or raw queries part in laravel, basically starting from basic to very complex queries. Please reply
I would suggest to start with default Laravel documentation for Eloquent, Query Builder and Collections, there are plenty of core stuff to read. And then later choose some of the advanced courses, like mine: laraveldaily.teachable.com/p/laravel-eloquent-expert-level
1, table shouldn't contain repetition, so first column should be extracted. 2. second column is id, primary key. 3 Job invoice amount should be followed by currency or unit. This requires a separate table with units and price per unit. 4. Job status is repetitive again, should have a job_status table and foreingn keyed here. This issue is bleeding from many wounds.
I didn't change anything in the database *structure*, I just helped with the current structure. Agree it could be optimized, but this video was about reporting, not about the DB structure.
Thanks lot fan of your code. Finding another way to do this public function report(){ $job = Jobcard::select([ 'job_comp_code', 'job_eng_no', 'job_invoice_amount', \DB::raw("DATE_FORMAT(job_received_date,'%Y-%m') as month") ])->get(); $job_report = $job->groupBy(['month','job_comp_code']); $job_code = $job->pluck('job_comp_code') ->sortBy('job_comp_code') ->unique(); return view('job',compact('job_report','job_code')); } ---->This is Controller function below is Blade view Month @foreach($job_code as $val) Job Amount {{$val}} Job count {{$val}} @endforeach @foreach($job_report as $month => $val) {{ \Carbon\Carbon::parse($month)->format('F Y') }} @foreach($job_code as $cval) {{(!empty($val[$cval]))? $val[$cval]->sum('job_invoice_amount'):0 }} {{ (!empty($val[$cval]))?count($val[$cval]):0 }} @endforeach @endforeach