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

Djbethel's avatar

Need Help With Unorthodox Pivot Table

It is my understanding that a pivot table should only be used with many to many relationships. But here's my problem.

I have a 'packages' table that stores general data about the different pricing packages my site offers. Then I have an employer table, which is a user type.

What I want to do is have a table called 'subscriptions', that after I process payment, it adds an entry to columns 'package_id' and 'employer_id'.

What I want to do ultimately is be able to do stuff like $employer->subscription->name, where the subscription table "acts" as a pivot table, hosting the relationship between employer and package.

The only problem is that this is not a many to many relationship. A package can belong to many different employers, except an employer can only have one subscription (so only one package)

Can any one help me implement this? Or at least tell me if this is even a logical approach. Better suggestions are welcome

0 likes
6 replies
martinbean's avatar

@Djbethel It seems a subscription can have many employers, and an employer belongs to a subscription. Therefore, this is a one-to-many relation, not a many-to-many relation.

Djbethel's avatar

I noted that it was not a many to many relationship.

This is what I want ultimately.

I want a packages table with the package information. Price, name and so on.

I want an employer table with the employers information.

I want a subscription table which holds the package_id and employer_id

When an employer purchases a subscription, I would like the package_id and employer_id to go into the subscriptions table.

Then I want to be able to do stuff like $employer->package->name and $employer->subscription->started_at

arabsight's avatar

you can have a one-to-one relation between Employer and Subscription, a one-to-many relation between Package and Subscription.

class Employer extends Model {

    public function subscription()
    {
        return $this->hasOne(Subscription::class);
    }
}
class Package extends Model {

    public function subscriptions()
    {
        return$this->hasMany(Subscription::class);
    }
}
class Subscription extends Model {

    public function employer()
    {
        return $this->belongsTo(Employer::class);
    }

    public function package()
    {
        return $this->belongsTo(Package::class);
    }
}
Djbethel's avatar

Ah, I got that code to work. Thanks a lot.

However is there a better way to implement it than I'm doing currently?

$package = Package::find(1);
    $employer = Employer::find(1);

    $subscription = new Subscription;
    $subscription->package_id = $package->id;
    $employer->subscription()->save($subscription);
arabsight's avatar

its fine, you can refactor it

$package = Package::find(1);
$employer = Employer::find(1);
$employer->subscription()->save(new Subscription(['package_id' => $package->id]));

Please or to participate in this conversation.