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

时间:2019-10-02 10:38:35

标签: laravel eloquent laravel-5.8

他试图通过雄辩的多对多关系展示与员工相关的角色,但它说:在模型[App \ Employee]上调用未定义的关系[role]。我已经从网上应用了很多解决方案,但是没有人为我工作

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

4 个答案:

答案 0 :(得分:1)

根据您的关系名称进行首次更改查询:

$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

答案 1 :(得分:0)

您已经按照关系定义了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');
} 

答案 2 :(得分:0)

您错误地使用了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

更新

将数据透视表名称从role_employee更改为employee_role,然后刷新您的迁移。

更新2

员工角色是集合,因此应该在foreach循环之内

答案 3 :(得分:0)

在您的控制器中

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

模范员工

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

在您的Relationships.many-to-many.employee.index文件中,编写类似表示您的视图文件的代码

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