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

Sushiarkt's avatar

Pivot table with an extra column

Hi guys,

I have three tables: options, groups and prices. Price table should contain: option id, group id and value; and I should be able to call something like this:

Option::find($option_id)->group($group_id)->price

But there shouldn't be any relationship between Option and Group model, everything should work over Price table.

Is that even possible? If so, any suggestions what should i look up.

Thanks a lot!

0 likes
2 replies
ahmeddabak's avatar
Level 47
  • in Option model create a many to many relation with the Group model
// in Option.php

public function groups() {
    return $this->belondsToMany(Group::class)->withPivot(['price']);
}
  • in the pivot table group_option add both primary keys and save the price attribute
  • then in you controller
$price = Option::find($option_id)->groups()->where('id',$group_id)->first()->pivot->price
1 like

Please or to participate in this conversation.