【发布时间】:2019-08-14 09:09:34
【问题描述】:
我正在尝试将数据从 db 获取到单个数组中
这是我编辑的代码,感谢 Matt C
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}]
print_r 我得到了什么
( [items:protected] => Array ( [0] => stdClass Object ( [userid] => 1 [pleads] => 2 [mleads] => 1 [aleads] => 1 ) ) ) ( [items:protected] => Array ( [0] => stdClass Object ( [userid] => 1 [pleads] => 0 [mleads] => 0 [aleads] => 0 ) ) ) ( [items:protected] => Array ( [0] => stdClass Object ( [userid] => 1 [pleads] => 0 [mleads] => 0 [aleads] => 0 ) ) ) ( [items:protected] => Array ( [0] => stdClass Object ( [userid] => 1 [pleads] => 1 [mleads] => 0 [aleads] => 0 ) ) ) ( [items:protected] => Array ( [0] => stdClass Object ( [userid] => 1 [pleads] => 1 [mleads] => 0 [aleads] => 0 ) ) ) ( [items:protected] => Array ( [0] => stdClass Object ( [userid] => 1 [pleads] => 1 [mleads] => 0 [aleads] => 0 ) ) ) ( [items:protected] => Array ( [0] => stdClass Object ( [userid] => 1 [pleads] => 0 [mleads] => 0 [aleads] => 0 ) ) )
非常感谢
【问题讨论】:
-
你没有在任何地方使用
$day。您只是一遍又一遍地获取完全相同的数据并将其放在同一个地方 -
你的 print_r 值是多少?
-
抱歉重新编辑。 > 它张贴在上面