调用模型 [App\Employee] 上的未定义关系 [角色]

Call to undefined relationship [role] on model [App\Employee]

您好,我试图显示分配给使用 eloquent 多对多关系的员工的角色,但它说:调用模型 [App\Employee] 上的未定义关系 [角色]。我从网上应用了很多解决方案,但没有人适合我

  public function index()
  {
    $employees = Employee::with('role')->get();
    return view('relations.many-to-many.employee.index', compact('employees'));

   @foreach($employees as $employee)
                <tr>
                  <td>
                    {{ $employee->employee_name }}
                  </td>
                  <td>
                    {{ $employee->role->role_name }}
                  </td>

                </tr>
                @endforeach  

模范员工

   public function roles()
   {
    return $this->belongsToMany(Role::class, 'role_employee');
    }

模范角色

  public function employees()
  {
    return $this->belongsToMany(employee::class, 'role_employee');
  }

员工迁移

  Schema::create('employees', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('employee_name');
        $table->timestamps();
    });

角色

 Schema::create('roles', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('role_name');
        $table->timestamps();
    });

role_employee

    Schema::create('role_employee', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->integer('employee_id')->unsigned();
        $table->integer('role_id')->unsigned();
        $table->timestamps();
    });

您已将 roles 方法定义为关系和访问 role

试试这个。

$employees = Employee::with('roles')->get();


@foreach($employees as $employee)
  <tr>
     <td>
         {{ $employee->employee_name }}
     </td>
     <td>
         {{ implode(', ', $employee->roles->pluck('role_name ')->toArray()) }}
     </td>
  </tr>
@endforeach 

模范员工

public function roles(){
  return $this->belongsToMany(Role::class, 'role_employee','employee_id','role_id');
}

模特角色

public function employees() {
  return $this->belongsToMany(Employee::class,'role_employee','role_id','employee_id');
} 

您错误地使用了 role 而不是 roles

$employees = Employee::with('roles')->get();
@foreach($employees as $employee)
            <tr>
              <td>
                {{ $employee->employee_name }}
              </td>
              <td>
               @foreach($employee->roles as $role)
               {{$role->role_name}}
               @endforeach
              </td>

            </tr>
@endforeach

更新

将 pivot table 名称从 role_employee 更改为 employee_role,然后刷新迁移。

更新 2

员工角色是集合,所以它应该在 foreach 循环中

首先根据您的关系名称更改查询:

$employees = Employee::with('roles')->get();

由于您拥有多对多关系,因此一名员工可以担任多个角色, 在您的视图中按以下方式更改您的代码

@foreach($employees as $employee)
<tr>
  <td>
    {{ $employee->employee_name }}
  </td>
  <td>
    @foreach($employees->roles as $role)
      {{ $role->role_name }} <br>
    @endforeach
  </td>
</tr>
@endforeach

编辑: 如下更新您的模态 员工模型

 public function roles(){
      return $this->belongsToMany(Role::class, 'role_employee','employee_id','role_id');
    }

榜样

 public function employees() {
      return $this->belongsToMany(Employee::class,'role_employee','role_id','employee_id');
    } 

参考:https://laravel.com/docs/6.x/eloquent-relationships#many-to-many-polymorphic-relations