Ok, so I'm trying to create a polymorphic relationship in Laravel that has four different types. The problem I have is that two of those levels are one-to-many and the other two are many-to-many. Here's what I have so far:
class Holiday extends AbstractModel {
protected $table = 'holidays';
protected $fillable = [
'scope_id', 'scope_type', 'main_date', 'alt_date', 'comp_date', 'name', 'disabled'
];
public function scope() {
return $this->morphTo();
}
}
class Country extends AbstractModel {
protected $table = 'countries';
protected $fillable = ['name', 'iso_code', 'disabled'];
public function holidays() {
return $this->morphMany("App\holidays", "scope");
}
}
class CountrySubdivision extends AbstractModel {
protected $table = 'countries_subdivisions';
protected $fillable = ['name', 'iso_code', 'countries_id', 'disabled'];
public function holidays() {
return $this->morphMany("App\holidays", "scope");
}
}
This is good for the first two levels which are one-to-many relationships. However, my problem lies in the last two levels which are many-to-many. Here are my models:
class Site extends AbstractModel {
protected $fillable = ['name', 'code', 'division', 'absence', 'unpaid'];
public function holidays() {
return $this->morphToMany("App\Holiday", "scope");
}
}
class Employee extends AbstractModel {
protected $fillable = [
'name', 'code', 'division', 'absence', 'unpaid'
];
public function holidays() {
return $this->morphToMany("App\Holiday", "scope");
}
}
The problem I have is when defining the inverse of the relationship. the morphTo
method is used for one-to-many relationship. However, for many-to-many, I need to use the morphedByMany()
method. however, I don't know how to use two different qualifiers for the same relationship.
Anyone know how to do this?