我有用户表,可以在其中将每个用户分配给一个经理。我有3种类型的经理(国家经理,区域经理和船队经理)。一位地区经理可以有许多车队经理和国家/地区经理,一位地区经理可以有许多车队经理。我的用户表具有在用户表的ID上引用的Fleet_manager_id,country_manager_id,area_manager_id列。问题是我不知道如何在同一张表上设置多态关系。 迁移:
Schema::table('users', function (Blueprint $table) {
$table->integer('fleet_manager_id')->unsigned()->nullable();
$table->foreign('fleet_manager_id')->references('id')->on('users');
$table->integer('country_manager_id')->unsigned()->nullable();
$table->foreign('country_manager_id')->references('id')->on('users');
$table->integer('area_manager_id')->unsigned()->nullable();
$table->foreign('area_manager_id')->references('id')->on('users');
});
我的用户模型:
public function fleetManagers()
{
return $this->hasMany(User::class, 'fleet_manager_id');
}
public function countryManager()
{
return $this->belongsTo(User::class, 'country_manager_id');
}
public function areaManager()
{
return $this->belongsTo(User::class, 'area_manager_id');
}
答案 0 :(得分:0)
所以,这是我到目前为止的解决方案:
public function managedByArea()
{
return $this->hasMany(User::class, 'area_manager_id');
}
public function area_manager()
{
return $this->belongsTo(User::class, 'area_manager_id');
}
public function managedByCountry()
{
return $this->hasMany(User::class, 'country_manager_id');
}
public function country_manager()
{
return $this->belongsTo(User::class, 'country_manager_id');
}
public function managedByFleet()
{
return $this->hasMany(User::class, 'fleet_manager_id');
}
public function fleet_manager()
{
return $this->belongsTo(User::class, 'fleet_manager_id');
}