【问题标题】:Laravel - production.ERROR: ErrorException: Trying to get property 'created_at' of non-objectLaravel - production.ERROR: ErrorException: Trying to get property 'created_at' of non-object
【发布时间】:2021-03-22 10:09:40
【问题描述】:

在 Laravel-5.8 中,我有这个代码:

$yearStatus = HrLeaveRequest::select('created_at')->where('employee_id', $userEmployee)->whereIn('leave_status', array(1, 3, 4))->orderBy('created_at', 'DESC')->first()->created_at;
//Get Specific current year from date
$specificYear = Carbon::createFromFormat('Y-m-d H:i:s', $yearStatus)->format('Y');
//Get Current Year
$currentYear = Carbon::now()->format('Y');
$currentDate = Carbon::now()->format('Y-m-d');

if ($currentYear > $specificYear){
    $currentstatus = HrLeaveRequest::select('leave_status')->where('employee_id', $userEmployee)->whereIn('leave_status', [1, 3, 4])->orderBy('created_at', 'DESC')->first();
  }else{
    $currentstatus = HrLeaveRequest::select('leave_status')->where('employee_id', $userEmployee)->whereDate('resumption_date', '<=', $currentDate)->whereIn('leave_status', [1, 3, 4])->orderBy('created_at', 'DESC')->first();
  }

无论何时:

$yearStatus = HrLeaveRequest::select('created_at')->where('employee_id', $userEmployee)->whereIn('leave_status', array(1, 3, 4))->orderBy('created_at', 'DESC')->first()->created_at;

为空,我收到此错误:

production.ERROR: ErrorException: Trying to get property 'created_at' of non-object

它指向:

$yearStatus = HrLeaveRequest::select('created_at')->where('employee_id', $userEmployee)->whereIn('leave_status', array(1, 3, 4))->orderBy('created_at', 'DESC')->first()->created_at;

我该如何解决这个问题?

谢谢

【问题讨论】:

    标签: laravel


    【解决方案1】:

    您在数据库中没有使用该过滤器的记录:

    $yearStatus = HrLeaveRequest::select('created_at')
      ->where('employee_id', $userEmployee)
      ->whereIn('leave_status', array(1, 3, 4))
      ->orderBy('created_at', 'DESC')
      ->first();
    
    if (!$yearStatus) {
      dd($yearStatus); // null
    }
    
    $yearStatus = $yearStatus->created_at;
    

    尝试在数据库中发送相同的选择以检查数据是否存在。

    first() - 返回结果的第一条记录,如果结果中没有数据,则返回 null。 您应该在使用前检查 first() 之后返回的数据。

    【讨论】:

      【解决方案2】:

      你应该从数据库中获取记录,然后确保它不为空,然后获取字段的 created_at:

       $record= HrLeaveRequest::select('created_at')->where('employee_id', $userEmployee)->whereIn('leave_status', array(1, 3, 4))
      ->orderBy('created_at', 'DESC')->first();
      
      if($record!=null)
      {
      
          $record->created_at;
          //Get Specific current year from date
      $specificYear = Carbon::createFromFormat('Y-m-d H:i:s', $yearStatus)->format('Y');
      //Get Current Year
      $currentYear = Carbon::now()->format('Y');
      $currentDate = Carbon::now()->format('Y-m-d');
      
      if ($currentYear > $specificYear){
          $currentstatus = HrLeaveRequest::select('leave_status')->where('employee_id', $userEmployee)->whereIn('leave_status', [1, 3, 4])->orderBy('created_at', 'DESC')->first();
        }else{
          $currentstatus = HrLeaveRequest::select('leave_status')->where('employee_id', $userEmployee)->whereDate('resumption_date', '<=', $currentDate)->whereIn('leave_status', [1, 3, 4])->orderBy('created_at', 'DESC')->first();
        }
      
      }
      

      当 $record 也为空时,您应该管理它...

      【讨论】:

        【解决方案3】:

        只需从查询 created_at 中删除 ->first()

        $yearStatus = HrLeaveRequest::select('created_at')->where('employee_id', $userEmployee)->whereIn('leave_status', array(1, 3, 4))->orderBy('created_at', 'DESC')->first();
        

        【讨论】:

          猜你喜欢
          • 2019-11-05
          • 1970-01-01
          • 2014-12-21
          • 1970-01-01
          • 1970-01-01
          • 2021-09-23
          • 1970-01-01
          • 2021-01-13
          • 2023-02-24
          相关资源
          最近更新 更多