title: Laravel - production.ERROR: ErrorException: Trying to get property 'created_at' of non-object
body: <p>In Laravel-5.8, I have this code:</p>
  <pre><code>$yearStatus = HrLeaveRequest::select('created_at')-&gt;where('employee_id', $userEmployee)-&gt;whereIn('leave_status', array(1, 3, 4))-&gt;orderBy('created_at', 'DESC')-&gt;first()-&gt;created_at;
  //Get Specific current year from date
  $specificYear = Carbon::createFromFormat('Y-m-d H:i:s', $yearStatus)-&gt;format('Y');
  //Get Current Year
  $currentYear = Carbon::now()-&gt;format('Y');
  $currentDate = Carbon::now()-&gt;format('Y-m-d');
  
  if ($currentYear &gt; $specificYear){
      $currentstatus = HrLeaveRequest::select('leave_status')-&gt;where('employee_id', $userEmployee)-&gt;whereIn('leave_status', [1, 3, 4])-&gt;orderBy('created_at', 'DESC')-&gt;first();
    }else{
      $currentstatus = HrLeaveRequest::select('leave_status')-&gt;where('employee_id', $userEmployee)-&gt;whereDate('resumption_date', '&lt;=', $currentDate)-&gt;whereIn('leave_status', [1, 3, 4])-&gt;orderBy('created_at', 'DESC')-&gt;first();
    }
  </code></pre>
  <p>Whenever:</p>
  <pre><code>$yearStatus = HrLeaveRequest::select('created_at')-&gt;where('employee_id', $userEmployee)-&gt;whereIn('leave_status', array(1, 3, 4))-&gt;orderBy('created_at', 'DESC')-&gt;first()-&gt;created_at;
  </code></pre>
  <p>is null, I got this error:</p>
  <blockquote>
  <p>production.ERROR: ErrorException: Trying to get property 'created_at' of non-object</p>
  </blockquote>
  <p>and it points at:</p>
  <pre><code>$yearStatus = HrLeaveRequest::select('created_at')-&gt;where('employee_id', $userEmployee)-&gt;whereIn('leave_status', array(1, 3, 4))-&gt;orderBy('created_at', 'DESC')-&gt;first()-&gt;created_at;
  </code></pre>
  <p>How do I resolve this?</p>
  <p>Thanks</p>