我有Session::get('employee_leave_tag');
,其中包含payroll_leave_employee_id
,因为data
不仅是我使用foreach
循环的 public function get_session_leave_tagv2()
{
$employee = [0 => 0];
if(Session::has('employee_leave_tag'))
{
$employee = Session::get('employee_leave_tag');
}
$empdata = array();
foreach($employee as $emp)
{
$employee_id = Tbl_payroll_leave_employeev2::select('payroll_employee_id')
->join('tbl_payroll_leave_schedulev2','tbl_payroll_leave_employee_v2.payroll_leave_employee_id','=','tbl_payroll_leave_schedulev2.payroll_leave_employee_id')
->where('tbl_payroll_leave_schedulev2.payroll_leave_employee_id',$emp)
->distinct()
->get();
if(count($employee_id) == 0)
{
$empa = Tbl_payroll_employee_basic::join('tbl_payroll_leave_employee_v2','tbl_payroll_leave_employee_v2.payroll_employee_id','=','tbl_payroll_employee_basic.payroll_employee_id')->whereIn('tbl_payroll_leave_employee_v2.payroll_leave_employee_id',$emp)->get();
array_push($empdata,$empa);
}
else
{
$empb = Tbl_payroll_leave_schedulev2::getallemployeeleavedata($employee_id)->get();
array_push($empdata,$empb);
}
}
$data['new_record'] = $empdata;
return json_encode($data);
}
。但我有一个错误
为foreach()提供的参数无效
有人可以帮我解决导致此错误的原因吗?感谢。
$array = [];
if($array == []) {
// array is empty
}
答案 0 :(得分:1)
您需要在代码中进行两处更改。
第一次更改:
您的数组声明$employee = array();
或$employee[];
第二次更改:
在编写if
循环之前,使用foreach
条件检查变量是否为数组并且不为空是一个很好的做法。它将使我们免于您所面临的错误。
if(is_array($employee) && !empty($employee))
{
foreach($employee as $emp)
{
}
}
答案 1 :(得分:0)
尝试这个...
{ $group: null, count: {$sum: 1} }