php laravel从foreach返回所有数据

时间:2019-03-23 15:52:01

标签: php mysql arrays laravel

我正在尝试将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  ) ) )

非常感谢您

1 个答案:

答案 0 :(得分:1)

无需使用雄辩的关系并重新使用代码,您应该能够使用selectSub获得计数。

$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('user')         
        ->select('users.id')
        ->selectSub($pleads, 'pleads')
        ->selectSub($mleads, 'mleads')
        ->selectSub($aleads, 'aleads')
        ->get()

您应该为每个用户找回类似的信息。

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

尽管有很多方法可以做到这一点

有雄辩的关系

User::withCount([
        'leads as pleads' => function ($q) {
            $q->where('lead_source_id', 7)
                ->whereRaw("DATE(created_at) = '$day'");
        },
        'leads as mleads' => function ($q) {
            $q->where('lead_source_id', 3)
                ->whereRaw("DATE(created_at) = '$day'");
        },
        'leads as aleads' => function ($q) {
            $q->where('lead_source_id', 4)
                ->whereRaw("DATE(created_at) = '$day'");
        },
    ])
    ->first()
    ->only(['id', 'pleads','mleads', 'aleads'])