noblemfd
3 months ago
765
3
Laravel

ERROR: Call to a member function fullName() on null

Posted 3 months ago by noblemfd

In my Laravel-5.8 project, I am trying to access the show view blade

Controller

public function show($id)
{
    if (! Gate::allows('employee_show')) {
        return abort(401);
    }  
    try { 
        
        return view('hr.employees.show', ['employee' => HrEmployee::findOrFail($id)]);
    } catch (Exception $exception) {
        Session::flash('error', 'Action failed! Please try again');
        return back();
        }                  
}

Model

class HrEmployee extends Model
{
  public $timestamps = false;
  protected $table = 'hr_employees';
  protected $primaryKey = 'id';

  protected $fillable = [
              'id',
              'employee_code',
              'company_id',
              'email',
              'line_manager_id',
              'employee_designation_id',
              'employee_grade_level_id',
              'employee_category_id',
              'employee_type_id',
              'employement_type_id',
              'work_location_id',
              'employment_date',
              'first_name',
              'last_name',
              'local_government_id',
              'nationality_id',
              'other_name',
              'is_hod',
              'department_id',
          ];

  public function linemanager()
  {
    return $this->hasOne('App\Models\Hr\HrEmployee', 'id', 'line_manager_id');
  }     

  public function fullName()
  {
    return $this->first_name . ' ' . $this->other_name . ' ' . $this->last_name;
  }
}

view

<h3 class="profile-username text-center">{{$employee->first_name}} {{$employee->last_name}}</h3>

  <p class="text-muted text-center">Designation: {{isset($employee->designation) ? $employee->designation->designation_name : 'N/A'}}</p>

 <ul class="list-group list-group-unbordered mb-3">
              <li class="list-group-item">
                <b>Staff ID:</b> <a class="float-right">{{isset($employee->employee_code) ? $employee->employee_code : 'N/A'}}</a>
              </li>
              <li class="list-group-item">
                <b>Department:</b> <a class="float-right">{{isset($employee->department) ? $employee->department->dept_name : 'N/A'}}</a>
              </li>
              <li class="list-group-item">
                <b>Grade Level:</b> <a class="float-right">{{isset($employee->gradelevel) ? $employee->gradelevel->grade_level_name : 'N/A'}}</a>
              </li>
               <li class="list-group-item">
                 <b>Line Manager:</b> <a class="float-right">{{ $employee->linemanager->fullName() ?? 'None' }}</a>
               </li>  
              <li class="list-group-item">
                <b>HOD:</b> <a class="float-right">{{isset($employee->department->depthead) ? $employee->department->depthead->first_name. ' ' .$employee->department->depthead->last_name : 'None'}}</a>
              </li>                     
 </ul>

When I rendered the view, I got this error:

ERROR: Call to a member function fullName() on null

I found out that whenever any employee doesn't have a line manager, the error comes up

<b>Line Manager:</b> <a class="float-right">{{ $employee->linemanager->fullName() ?? 'None' }}</a>

How do I get it resolved?

Thank you

Please sign in or create an account to participate in this conversation.