Eloquent Practice: Group By Month and Company

Поделиться
HTML-код
  • Опубликовано: 1 окт 2024
  • Let's help one person on Laracasts forum to group data in Eloquent, also I will explain my way of thinking when it comes to grouping and reporting.
    Original post from Laracasts: laracasts.com/...
    My 4-hour course "Eloquent: Expert Level" bit.ly/eloquent...

Комментарии • 28

  • @nabeelyousafpasha
    @nabeelyousafpasha 4 года назад +4

    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.

  • @IlPandax
    @IlPandax 4 года назад +4

    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.

    • @PovilasKorop
      @PovilasKorop 4 года назад +4

      Not much difference, it's just a personal preference, whether you're more used to working with arrays or with collection methods.

  • @antobine
    @antobine 3 года назад +1

    Great video as usual! Where could I find more info on use (&$report ) you mention? Thanks!!

  • @brunonogueira1743
    @brunonogueira1743 3 года назад +1

    Safe my life programador of laravel

  • @ZoBuz
    @ZoBuz 6 месяцев назад

    how to calculate the sum of Job Amount A and Job Amount B.

  • @guillermocava3568
    @guillermocava3568 4 года назад +1

    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.

  • @khokonchandra9234
    @khokonchandra9234 2 года назад

    I need help on selectRaw, whereColumn, whereRaw, groupBy, and Nested Sub query using Eloquent ORM.

  • @emadabulebda5573
    @emadabulebda5573 4 года назад +1

    Great!
    We need more videos like that

  • @glyakk
    @glyakk Год назад

    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.

  • @martinjones8231
    @martinjones8231 3 года назад

    Thank you very much it works for me but it not counting for me why plz.

  • @punsineeto6058
    @punsineeto6058 3 года назад

    thank a lot! this video is very helpful.

  • @paulfontaine7819
    @paulfontaine7819 4 года назад

    Not controversial for me, I did it the same way in a similar case.

  • @Paltibenlaish
    @Paltibenlaish 4 года назад

    the problem that I have working with the collections after the query is that I cannot use pagination anymore :(
    or how you would do pagination?

    • @PovilasKorop
      @PovilasKorop 4 года назад +2

      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.

  • @KamranBhutto
    @KamranBhutto 2 года назад

    nice work Thanks

  • @elfaztube5378
    @elfaztube5378 3 года назад

    Perfect

  • @behzodjon
    @behzodjon 4 года назад

    Great solution!Respect!

  • @paljinder3
    @paljinder3 3 года назад

    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

  • @randomguy5922
    @randomguy5922 4 года назад

    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

    • @PovilasKorop
      @PovilasKorop 4 года назад

      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

    • @randomguy5922
      @randomguy5922 4 года назад

      @@PovilasKorop Thanks for ur reply, u r really helping us a lot.

  • @iarman8474
    @iarman8474 4 года назад

    Great work. I just wanted to know if we can Biometric integration with laravel.

    • @xylatescripts406
      @xylatescripts406 4 года назад

      Someone posted a package on laravels reddit page for fingerprint logins

  • @dgloria
    @dgloria 4 года назад

    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.

    • @qAntBcn
      @qAntBcn 4 года назад +2

      bla bla... Show me the code!

    • @PovilasKorop
      @PovilasKorop 4 года назад +5

      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.

    • @DanMlayah
      @DanMlayah 4 года назад

      @@qAntBcn well said. Show the code, explain later with optimization tricks. Someone said its easier to criticize than to create.