1. <tfoot id='n7gY0'></tfoot>

    <small id='n7gY0'></small><noframes id='n7gY0'>

        <bdo id='n7gY0'></bdo><ul id='n7gY0'></ul>
    1. <legend id='n7gY0'><style id='n7gY0'><dir id='n7gY0'><q id='n7gY0'></q></dir></style></legend>
    2. <i id='n7gY0'><tr id='n7gY0'><dt id='n7gY0'><q id='n7gY0'><span id='n7gY0'><b id='n7gY0'><form id='n7gY0'><ins id='n7gY0'></ins><ul id='n7gY0'></ul><sub id='n7gY0'></sub></form><legend id='n7gY0'></legend><bdo id='n7gY0'><pre id='n7gY0'><center id='n7gY0'></center></pre></bdo></b><th id='n7gY0'></th></span></q></dt></tr></i><div id='n7gY0'><tfoot id='n7gY0'></tfoot><dl id='n7gY0'><fieldset id='n7gY0'></fieldset></dl></div>

      Laravel 使用 Sum 和 Groupby

      时间:2023-09-22
            <tbody id='haTwp'></tbody>

        1. <i id='haTwp'><tr id='haTwp'><dt id='haTwp'><q id='haTwp'><span id='haTwp'><b id='haTwp'><form id='haTwp'><ins id='haTwp'></ins><ul id='haTwp'></ul><sub id='haTwp'></sub></form><legend id='haTwp'></legend><bdo id='haTwp'><pre id='haTwp'><center id='haTwp'></center></pre></bdo></b><th id='haTwp'></th></span></q></dt></tr></i><div id='haTwp'><tfoot id='haTwp'></tfoot><dl id='haTwp'><fieldset id='haTwp'></fieldset></dl></div>
            <legend id='haTwp'><style id='haTwp'><dir id='haTwp'><q id='haTwp'></q></dir></style></legend>
          • <tfoot id='haTwp'></tfoot>

              <bdo id='haTwp'></bdo><ul id='haTwp'></ul>

              1. <small id='haTwp'></small><noframes id='haTwp'>

                本文介绍了Laravel 使用 Sum 和 Groupby的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我想获取每个月的数量总和,以便我可以在条形图上显示每月的数量

                I would like to fetch sum of quantity in each month, so that I can display on bar chart quantities against month

                这就是我想的,但没有锻炼

                This is what I thought but didn't workout

                 $data1 = Borrow::groupBy(function($d) {
                 return Carbon::parse($d->created_at)->format('m')->sum('quantity');
                 })->get();
                

                我的表结构

                Schema::create('borrows', function (Blueprint $table) {
                        $table->increments('id');
                        $table->integer('member_id');
                        $table->integer('book_id');
                        $table->integer('quantity');
                        $table->integer('status')->default(0);
                        $table->timestamps();
                    });
                

                推荐答案

                that a collection group by not an eloquent groupby

                that a collection group by not an eloquent groupby

                如果你想用 eloquent 来做,必须:

                if you want to do it with eloquent, gotta:

                 $data1 = Borrow::selectRaw('SUM(quantity) as qt, MONTH(created_at) as borrowMonth')
                      ->groupBy('borrowMonth')->get();
                

                如果你想用集合 groupBy 方法来做,你应该先做 get,然后是 groupBy.

                if you want to do it with the collection groupBy method, you should first do the get, then the groupBy.

                就像,虽然我不确定你在回调中所做的事情要完成什么..

                As in, though im not sure what you're trying to accomplish with what you do inside the callback..

                 $data1 = Borrow::get()->groupBy(function($d) {
                 return Carbon::parse($d->created_at)->format('m')->sum('quantity');
                 });
                

                这篇关于Laravel 使用 Sum 和 Groupby的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Laravel (HasMany) 不检索值 下一篇:laravel whereIn 多列

                相关文章

                最新文章

                1. <tfoot id='xOazN'></tfoot>
                  <i id='xOazN'><tr id='xOazN'><dt id='xOazN'><q id='xOazN'><span id='xOazN'><b id='xOazN'><form id='xOazN'><ins id='xOazN'></ins><ul id='xOazN'></ul><sub id='xOazN'></sub></form><legend id='xOazN'></legend><bdo id='xOazN'><pre id='xOazN'><center id='xOazN'></center></pre></bdo></b><th id='xOazN'></th></span></q></dt></tr></i><div id='xOazN'><tfoot id='xOazN'></tfoot><dl id='xOazN'><fieldset id='xOazN'></fieldset></dl></div>

                  <legend id='xOazN'><style id='xOazN'><dir id='xOazN'><q id='xOazN'></q></dir></style></legend>

                  <small id='xOazN'></small><noframes id='xOazN'>

                    <bdo id='xOazN'></bdo><ul id='xOazN'></ul>