How to manage ManyToMany relation in TableBuilder and FormBuilder?

I have the following models
Product
  • id
  • name
Pricelist
  • id
  • name
ProductPrice
  • id
  • product_id
  • pricelist_id
  • price
I want to create a dynamic table on product listing page, where all the Pricelist are listed as columns (e.g. Retail, Reseller, Discount) and respectively the belonging product prices.

I defined these functions in Product model:
    public function prices(): HasMany
    {
        return $this->hasMany(ProductPrice::class, 'product_id');
    }

    public function price($pricelist_id)
    {
        return $this->prices()->where('pricelist_id', $pricelist_id)->first();
    }

Furthermore I tried this code in the ProductRelationsManager table columns function:
...array_map(
  fn(Pricelist $pricelist) => TextColumn::make('price_' . $pricelist->id)
->label($pricelist->name), $this->getOwnerRecord()->pricelists()->get()->all())

So it creates all the pricelist columns, but I have no clue what should I add to the TextColumn::make() parameter in order to reach the given price belonging to the actual pricelist.

Also have no clues how to define the form, what should I add to the TextInput::make() as parameter.
Was this page helpful?