gpt4 book ai didi

php laravel 从过去一周的数据库中获取数据

转载 作者:行者123 更新时间:2023-11-29 07:22:49 25 4
gpt4 key购买 nike

我正在尝试从数据库中获取数据到一个数组中

这就是我尝试过的 foreach 循环遍历第一周的所有日期并每次执行此查询以获取数据的方法

foreach ($list1 as &$day){
$pleads = \DB::table('leads')
->selectRaw('count(*)')
->whereColumn('owned_by_id', 'users.id')
->where('lead_source_id', 7)
->whereRaw("DATE(created_at) = '$day'");

$mleads = \DB::table('leads')
->selectRaw('count(*)')
->whereColumn('owned_by_id', 'users.id')
->where('lead_source_id', 3)
->whereRaw("DATE(created_at) = '$day'");

$aleads = \DB::table('leads')
->selectRaw('count(*)')
->whereColumn('owned_by_id', 'users.id')
->where('lead_source_id', 4)
->whereRaw("DATE(created_at) = '$day'");

$personalleads = \DB::table('users')
->where('id', $id) // User ID
->select('users.id')
->selectSub($pleads, 'pleads')
->selectSub($mleads, 'mleads')
->selectSub($aleads, 'aleads')
->get();
return $personalleads;
}

当我这样做时,我只得到 1 个输出,例如:

[{"userid":1,"pleads":2,"mleads":1,"aleads":1}]  

但我想要的结果在下面

[{"userid":1,"pleads":2,"mleads":1,"aleads":1},{"userid":1,"pleads":0,"mleads":0,"aleads":0},{"userid":1,"pleads":0,"mleads":0,"aleads":0},{"userid":1,"pleads":1,"mleads":0,"aleads":0},{"userid":1,"pleads":1,"mleads":0,"aleads":0},{"userid":1,"pleads":1,"mleads":0,"aleads":0},{"userid":1,"pleads":0,"mleads":0,"aleads":0}]

但我认为它们是多个对象,我无法将它们放入单个数组中

最佳答案

您将在第一次迭代时从循环中返回。相反,将结果存储在一个数组中,然后在循环外返回。

$results = [];
foreach ($list1 as &$day){
$personalleads = ....
array_push($results, personalleads);
}
return $results;

关于php laravel 从过去一周的数据库中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55316695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com