3 months ago

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


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


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

  protected $fillable = [

  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;


<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 class="list-group-item">
                <b>Department:</b> <a class="float-right">{{isset($employee->department) ? $employee->department->dept_name : 'N/A'}}</a>
              <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 class="list-group-item">
                 <b>Line Manager:</b> <a class="float-right">{{ $employee->linemanager->fullName() ?? 'None' }}</a>
              <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>

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.