F
Filamentβ€’7mo ago
morawcik

Pivot data in model/resource table

Hi. I have Product model with relation:
public function customerGroups(): BelongsToMany
{
return $this->belongsToMany(CustomerGroup::class, "customer_group_product")
->withPivot(['purchasable', 'visible', 'enabled', 'starts_at', 'ends_at'])->withTimestamps();
}
public function customerGroups(): BelongsToMany
{
return $this->belongsToMany(CustomerGroup::class, "customer_group_product")
->withPivot(['purchasable', 'visible', 'enabled', 'starts_at', 'ends_at'])->withTimestamps();
}
I tried to show purchasable in resource table but without success. I added column:
Tables\Columns\TextColumn::make('customerGroups.pivot.purchasable')
->badge()
->label('purchasable')
Tables\Columns\TextColumn::make('customerGroups.pivot.purchasable')
->badge()
->label('purchasable')
but it is always empty. When i change it to customerGroups.name the I get those names. I even tried to dump $state inside of color() method. I started with just customerGroups and I got this:
{"id":1,"name":"Detal","handle":"detal","default":1,"attribute_data":null,"created_at":"2023-07-31T21:53:13.000000Z","updated_at":"2023-08-02T20:48:21.000000Z","pivot":{"product_id":142,"customer_group_id":1,"purchasable":1,"visible":1,"enabled":1,"starts_at":null,"ends_at":null,"created_at":"2023-08-13T08:33:37.000000Z","updated_at":"2024-05-30T18:26:16.000000Z"}}
{"id":1,"name":"Detal","handle":"detal","default":1,"attribute_data":null,"created_at":"2023-07-31T21:53:13.000000Z","updated_at":"2023-08-02T20:48:21.000000Z","pivot":{"product_id":142,"customer_group_id":1,"purchasable":1,"visible":1,"enabled":1,"starts_at":null,"ends_at":null,"created_at":"2023-08-13T08:33:37.000000Z","updated_at":"2024-05-30T18:26:16.000000Z"}}
and then if i tried customerGroups.pivot or customerGroups.pivot.purchasable I got nothing - like dd() even not fired
2 Replies
Dan Harrin
Dan Harrinβ€’7mo ago
how about creating a hasmany to the pivot table then using hasmanyrelationname.purchasable
morawcik
morawcikOPβ€’7mo ago
That works, thanks πŸ™‚
Want results from more Discord servers?
Add your server