Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bar2's avatar
Level 8

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?

0 likes
8 replies
bar2's avatar
Level 8

Companies are require to define a quota for their services, with these parameters;

company_id, id of their company

service_period_id, predefined value of a period of serving time,

territory_id, the territory that the company will serve,

service_group_id, group of the specified service.

Given these 4 parameters, which are all many-to-many, a company can enter a quota for limiting their service traffic. I don't believe polymorphic relations suit this need.

d3xt3r's avatar
d3xt3r
Best Answer
Level 29

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

2 likes
bar2's avatar
Level 8

@premsaurav awesome solution! Will try it right away. And for the sake of completeness, if I were to lookup a quota value within the company model, how would I achieve it? (of course given other 3 parameters, or maybe a collection with 2)

d3xt3r's avatar

@bar2

class Quota extends Model {
    
    ...

    public function scopeAudit(Builder $query, Company $company = null, ...){

        if($company !== null)
            $query = $query->where($company->getKeyName(),$company->getKey());

        ...
    
        return $query;
    }

    ...
}

$audit = Quota::audit($company, ...)->get();

1 like
bar2's avatar
Level 8

@premsaurav For a debug seeding with a single company, single service group, 7 day of week, 15 territories being served, and for each (12) of service periods, the lookup table grows into ~1.3k.

With real data, it can grow up to millions of rows. Quick Math: 500 company x 2 service group x 7 day x 20 territory x 12 service period = ~ 1.6M

should I be worried regarding my solution?

d3xt3r's avatar

@bar2 If you really have such a requirement, do have proper indexing on your tables, you should be good.

Please or to participate in this conversation.