试图获取非对象问题的属性“名称”无法解决

时间:2019-08-31 08:21:48

标签: laravel laravel-5 laravel-orm

当我想显示我的员工资料并想从另一个表中添加部门和职位时,我面临着这个问题。我无法解决。.我看到了先前的问题,但无法解决我的答案。

控制器:

public function show($id)
{
    $data['title'] = "Profile";
    $data['employee'] = Employee::with(['department','designation'])->findOrFail($id);
    $data['departments'] = Department::orderBy('name')->pluck('name','id');
    $data['designations'] = Designation::orderBy('name')->pluck('name','id');
    return view('admin.employee.show',$data);
}

视图:

<li>
  <div class="title">Department:</div>
  <div class="text">{{ $employee->department->name }}</div>
</li>

型号:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Notifications\Notifiable;

class Employee extends Model
{
    use SoftDeletes;
    use Notifiable;
    protected $fillable = [
        'firstname','lastname', 'email', 'password','designation_id','department_id'];

    public function designation()
    {
        return $this->belongsTo(Designation::class);
    }
    public function department()
    {
        return $this->belongsTo(Department::class);
    }
}

部门模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Department extends Model
{
    use SoftDeletes;
    protected $fillable=['name','depatment_details'];


}

试图获取非对象的属性“名称”(查看:C:\ xampp \ htdocs \ hrm \ resources \ views \ admin \ employee \ show.blade.php)

1 个答案:

答案 0 :(得分:0)

尝试一下。

<li>
  <div class="title">Department:</div>
  <div class="text">{{ $data['employee']->department->name }}</div>
</li>