Your description is not precise enough to give you any solution. But you might want to take a look at polymorphic relations: https://laravel.com/docs/5.1/eloquent-relationships#polymorphic-relations
4-way pivot table
Hey guys,
My first post on the forum (and my first laravel experience), and since its partially/mostly Laravel related, I wanted to get some help from fellow developers here, so bear with me.
Models and relations are like this;
Company, a basic company model, ServicePeriod, time periods such as 14PM - 16PM that company serves in between, Territory, territory that company serves in, ServiceGroup, a group of services that the company provides.
Now I need to define a 4-way pivot table for these 4 models, and provide a quota for the given company, on a specific service period, for the territory, regarding the service group.
If I were to create a 4-way pivot table for these 4 tables/models, would I be able to set or get the values for the quota?
If so, how would my models would be attached to each other in terms of relations?
Yes polymorphic relation wont solve your purpose. Try to put your relation in word
A company has many quota(s)
class Company extends Model {
public function quotas () {
return $this->hasMany(Quota::class);
}
}
A quota belongs to a company, service_perriod, territory, service_group
class Quota extends Model {
public function company () {
return $this->belongsTo(Company::class);
}
public function servicePeriod () {
return $this->belongsTo(ServicePeriod::class);
}
public function territory () {
return $this->belongsTo(Territory::class);
}
public function serviceGroup () {
return $this->belongsTo(ServiceGroup::class);
}
}
to add a new Quota to a company
$quota = new Quota();
$quota->servicePeriod()->associate($servicePeriod);
$quota->territory()->associate($territory);
$quota->serviceGroup()->associate($serviceGroup);
// and finally
$quota->company()->associate($company);
$quota->save();
Please or to participate in this conversation.