【问题标题】:php laravel return all data from foreachphp laravel 从 foreach 返回所有数据
【发布时间】: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 值是多少?
  • 抱歉重新编辑。 > 它张贴在上面

标签: php mysql arrays laravel


【解决方案1】:

不使用 Eloquent 关系并重用您的代码,您应该能够使用 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'])

【讨论】:

  • 首先我要非常感谢您的回答我一直在到处寻找可以帮助我完成这项任务的人我得到了[{"id":1,"pleads":2,"mleads":1,"aleads":1}]但是这个代码在foreach里面我想返回@987654325 @ 而不是只有 1 行
  • 非常感谢,如果您帮助我解决 foreach 循环问题,我将不胜感激,因为我已经尝试连续 8 小时解决它,但没有成功
  • 现在应该在没有 foreach 的情况下工作并返回所有用户。
  • 谢谢,但问题是我必须使用 foreach,因为我想在上个月的每一天从 db 中检索数据,当我在 foreach 中使用 return 时,它只返回第一天我将编辑帖子
  • 你能把那部分代码贴出来吗?这部分只循环通过每个用户。
猜你喜欢
  • 2021-07-14
  • 2019-04-17
  • 2022-10-14
  • 2021-11-17
  • 1970-01-01
  • 2020-03-04
  • 1970-01-01
  • 2017-12-22
  • 1970-01-01
相关资源
最近更新 更多