【问题标题】:How to return the list of objects and their count如何返回对象列表及其计数
【发布时间】:2020-10-31 03:34:28
【问题描述】:

我想用对象列表替换 COUNT(ID)

我想返回一个包含对象列表的数组而不是返回

他们的人数

我的代码:

$data['myresponse'] = DB::table("products")
            ->where('id_advisor',$advisor->id)->where('deleted_at', NULL)
            ->where('created_at',">=", $start)->where('created_at',"<=", $end)
            ->select(DB::raw("DATE(created_at) as date,COUNT(id) objects"))
            ->groupBy(DB::raw('Date(created_at)'))
            ->get();

回复:

{
    "data": {
        "myresponse": [
            {
                "date": "2020-07-03",
                "objects": 2
            },
            {
                "date": "2020-07-05",
                "objects": 4
            },
            {
                "date": "2020-07-08",
                "objects": 8
            },
            {
                "date": "2020-07-09",
                "objects": 17
            }
        ]
    }
}

我需要的是返回这样的东西:

enter image description here

【问题讨论】:

  • 问题不清楚。请改写。
  • 什么是“对象”?
  • 这是项目的数量,我在刚刚添加的图像中更改了它,我想用特定键返回另一个数组中的计数元素忘记键'对象',我已将其更改为 'count',并且我希望 'objects' 包含计数的项目。

标签: php mysql sql json laravel


【解决方案1】:

我会从另一边接近它。选择您的对象并将它们放入日期桶中。

Laravel 可以帮助您开箱即用地根据属性对其进行分组。您只需要将其映射到所需的结构。

$products = DB::table("products")
    ->where('id_advisor',$advisor->id)->where('deleted_at', NULL)
    ->where('created_at',">=", $start)->where('created_at',"<=", $end)
    ->get()
    ->groupBy('created_at');

**代码示例的原始位** 下面是一些(伪)代码作为反向方法的示例。

$products = DB::table("products")
    ->where('id_advisor',$advisor->id)->where('deleted_at', NULL)
    ->where('created_at',">=", $start)->where('created_at',"<=", $end)
    ->get();
    
$buckets = $products->map(
  function($product) { 
    return $product->created_at; 
  }
)->unique();

$result = $buckets->map(function ($bucket) use ($products) {
    return [
      'date' => $bucket, 
      'products' => $products->filter(
        function($product) use ($bucket) { 
          return $product->created_at === $bucket;
        }
      )
    ];
});

dd($results);

转储结果:

Illuminate\Support\Collection {#1050
  #items: array:3 [
    0 => array:2 [
      "date" => "2020-07-03"
      "products" => Illuminate\Support\Collection {#1048
        #items: array:1 [
          0 => array:2 [
            "date" => "2020-07-03"
            "id" => 1
          ]
        ]
      }
    ]
    1 => array:2 [
      "date" => "2020-07-04"
      "products" => Illuminate\Support\Collection {#1047
        #items: array:2 [
          1 => array:2 [
            "date" => "2020-07-04"
            "id" => 2
          ]
          2 => array:2 [
            "date" => "2020-07-04"
            "id" => 3
          ]
        ]
      }
    ]
    3 => array:2 [
      "date" => "2020-07-05"
      "products" => Illuminate\Support\Collection {#1045
        #items: array:1 [
          3 => array:2 [
            "date" => "2020-07-05"
            "id" => 4
          ]
        ]
      }
    ]
  ]
}

【讨论】:

    【解决方案2】:

    试试这个

    $data['myresponse'] = DB::table("products")
                ->where('id_advisor',$advisor->id)->where('deleted_at', NULL)
                ->where('created_at',">=", $start)->where('created_at',"<=", $end)
                ->select(DB::raw("DATE(created_at) as date, DB::raw('count(*) as objects'))
                ->groupBy(DB::raw('DATE(created_at)')) // or ->groupBy('date')
                ->get();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-08
      • 2019-07-19
      • 2021-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多