【问题标题】:Laravel - ERROR: Call to a member function fullName() on nullLaravel - 错误:在 null 上调用成员函数 fullName()
【发布时间】:2020-07-16 23:05:25
【问题描述】:

在我的 Laravel-5.8 项目中,我正在尝试访问显示视图刀片

控制器

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();
        }                  
}

型号

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;
  }
}

查看

<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>

当我渲染视图时,我得到了这个错误:

错误:在 null 上调用成员函数 fullName()

我发现,只要任何员工没有直线经理,就会出现错误

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

如何解决?

谢谢

【问题讨论】:

    标签: laravel


    【解决方案1】:

    首先,您最好使用accessor 而不是获取全名的方法。像这样定义它:

    public function getFullNameAttribute()
    {
        return implode(' ', array_filter([$this->first_name, $this->other_name, $this->last_name])); 
    }
    

    作为额外的奖励,如果任何属性为空或为空,您将不会看到额外的“胶水”;

    然后在你的代码中调用它作为一个简单的属性:

    {{ $employee->linemanager->full_name ?? 'None' }}
    

    附:你需要return abort函数。无论如何它都会抛出异常。

    【讨论】:

      猜你喜欢
      • 2018-09-06
      • 1970-01-01
      • 2019-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多