如何从Laravel中的数据库传递值

时间:2018-08-13 09:49:01

标签: php laravel laravel-blade

如何将Lead_id的值传递到出勤表? 这是我的控制器中的代码

$scores = Score::with('lead','subject')->get()->groupBy('lead_id');

我想在这里传递$分数:

$atnds = DB::table('attendances')
    ->select(DB::raw('count(*) as total, status'))
    ->where('status', '=', 'P')
    ->whereBetween('days', [$from,$to])
    ->groupBy('status')
    **->where('lead_id','=',$scores)**
    ->get();

表和考勤表均具有lead_id,可以将它们连接起来吗?我想根据分数表中的lead_id执行总和等于出勤表。用我的代码,我得到了这个空数组enter image description here

2 个答案:

答案 0 :(得分:1)

where的条件很有说服力,希望您输入的字符串/整数值而不是集合,因为您尝试传递它。

从此查询获取结果后:

它们应按如下键进行分组:

$scores = Score::with('lead','subject')->get()->groupBy('lead_id');

Collection{
1 => Collection{somedata},
2 => Collection{somedata},
}

所以我请您只需要此集合中的键:

  $atnds = DB::table('attendances')
 ->select(DB::raw('count(*) as total, status'))
 ->where('status', '=', 'P')
 ->whereBetween('days', [$from,$to])
 ->groupBy('status')
 ->whereIn('lead_id',$scores->pluck('lead_id')->toArray())
 ->get();

这是DD $分数的结果

 "id" => 62
        "subject_id" => 4
        "lead_id" => 5
        "uuid" => "36c850d0-9ec9-11e8-85e1-cdb65cbc1965"
        "created_at" => "2018-08-13 07:19:27"
        "updated_at" => "2018-08-13 07:19:27"
      ]

总数不存在

答案 1 :(得分:1)

尝试一下:

$scores = Score::with('lead','subject')->get();

$atnds = DB::table('attendances')
                 ->select(DB::raw('count(*) as total, status'))
                 ->where('status', '=', 'P')
                 ->whereBetween('days', [$from,$to])
                 ->groupBy('status')
                 ->whereIn('lead_id', array_column($scores->toArray(), 'lead_id'))
                 ->get();