【问题标题】:How to get all month record count in laravel如何在laravel中获取所有月份的记录数
【发布时间】:2017-08-22 22:16:22
【问题描述】:

我已经使用 DB 查询通过 created_at 列应用了 group by,但我喜欢通过 laravel eloquent 来做到这一点

输出应该是这样的:-

Array
(
  [1] => 2
  [2] => 3
  [3] => 7
  [4] => 4
  [5] => 5
  [6] => 7
  [7] => 2
  [8] => 9
  [9] => 0
  [10] => 4
  [11] => 0
  [12] => 0
)

请提供任何帮助我做到这一点。

【问题讨论】:

标签: php mysql laravel-5


【解决方案1】:

如果你的 created_at 列是 DATETIME,你可以这样分组

Entity::orderBy(...)->groupBy(DB::raw('MONTH(created_at)'))

您也可以提前选择月份

Entity::select(DB::raw('MONTH(created_at) as month')->groupBy('month')->get()->keyBy('month');

【讨论】:

    【解决方案2】:

    请尝试以下步骤:-

    1. 运行 composer 安装包:composer 需要 nesbot/carbon
    2. 在您的代码之上:使用 Carbon\Carbon;

    假设我有模型用户,

    $users = User::select('id', 'created_at')
    ->get()
    ->groupBy(function($date) {
        //return Carbon::parse($date->created_at)->format('Y'); // grouping by years
        return Carbon::parse($date->created_at)->format('m'); // grouping by months
    });
    
    $usermcount = [];
    $userArr = [];
    
    foreach ($users as $key => $value) {
        $usermcount[(int)$key] = count($value);
    }
    
    for($i = 1; $i <= 12; $i++){
        if(!empty($usermcount[$i])){
            $userArr[$i] = $usermcount[$i];    
        }else{
            $userArr[$i] = 0;    
        }
    }
    

    希望它能帮助你制作这样的数组。

    【讨论】:

    • 哇伊斯梅尔,这是我正在寻找与 laravel eloquent 相同的工作,谢谢!
    • 最欢迎 :)
    【解决方案3】:

    如果我们想根据不同年份的每个月获取计数,我们使用以下代码

    return $usersPerMonth =User::select(DB::raw('count(id) as `data`'),DB::raw("DATE_FORMAT(created_at, '%Y-%m') new_date"))
            ->groupBy('new_date')->orderBy('new_date')->get();
    

    【讨论】:

      【解决方案4】:

      修改 Ismael Ansari 帖子,

       $users = User::select('id', 'created_at')
              ->get()
              ->groupBy(function ($date) {
                  return Carbon::parse($date->created_at)->format('m');
              });
      
          $usermcount = [];
          $userArr = [];
      
          foreach ($users as $key => $value) {
              $usermcount[(int)$key] = count($value);
          }
      
          $month = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
      
          for ($i = 1; $i <= 12; $i++) {
              if (!empty($usermcount[$i])) {
                  $userArr[$i]['count'] = $usermcount[$i];
              } else {
                  $userArr[$i]['count'] = 0;
              }
              $userArr[$i]['month'] = $month[$i - 1];
          }
      
          return response()->json(array_values($userArr));
      

      输出:

      [
          {
              "count": 1,
              "month": "Jan"
          },
          {
              "count": 0,
              "month": "Feb"
          },
          {
              "count": 0,
              "month": "Mar"
          },
          {
              "count": 0,
              "month": "Apr"
          },
          {
              "count": 0,
              "month": "May"
          },
          {
              "count": 0,
              "month": "Jun"
          },
          {
              "count": 0,
              "month": "Jul"
          },
          {
              "count": 0,
              "month": "Aug"
          },
          {
              "count": 0,
              "month": "Sep"
          },
          {
              "count": 0,
              "month": "Oct"
          },
          {
              "count": 0,
              "month": "Nov"
          },
          {
              "count": 0,
              "month": "Dec"
          }
      ]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-21
        相关资源
        最近更新 更多