使用json_encode()无法将类Illuminate \ Support \ Collection的对象转换为int

时间:2017-11-11 02:08:27

标签: php laravel

我有多个query joins条件,我试图将array从我的控制器传递到model然后传递{{1}查询结果。我收到这个错误:

  

类Illuminate \ Database \ Eloquent \ Collection的对象无法转换为int

有人可以帮我找到原因吗?

控制器

return

Tbl_payroll_leave_schedule.php

  public function get_session_leave_tagv2()
     {
          $employee = [0 => 0];
          if(Session::has('employee_leave_tag'))
          {
               $employee = Session::get('employee_leave_tag');
          }
          $emps = Tbl_payroll_leave_employeev2::select('payroll_employee_id')->whereIn('payroll_leave_employee_id',$employee)->get();

          $emp = Tbl_payroll_leave_schedulev2::getallemployeeleavedata($emps)->get();

          $data['new_record'] = $emp;

          return json_encode($data);
     }

1 个答案:

答案 0 :(得分:1)

如果您使用的是控制器,他们会避免使用手动json_encode,而是选择以下方式

public function get_session_leave_tagv2()
{
    $employee = [0 => 0];
    if(Session::has('employee_leave_tag'))
    {
       $employee = Session::get('employee_leave_tag');
    }
    $emps = Tbl_payroll_leave_employeev2::select('payroll_employee_id')->whereIn('payroll_leave_employee_id',$employee)->get();

    $emp = Tbl_payroll_leave_schedulev2::getallemployeeleavedata($emps)->get();

    $data['new_record'] = $emp;

    return response()->json($data);
}