Duplicate the row for many to many relationship in Model A List page

instead of showing ModelBs relationships with "," separating them, how can i tell filament to duplicate the whole row with the other relationship value. i tried custom column, it works fine if i kept it in the end where i can end the row as html, but is there a better way that i can achieve the above
7 Replies
Dan Harrin
Dan Harrin2y ago
are you using ReplicateAction or something?
Cybrarist
CybraristOP2y ago
replicate will create another record, what i am looking to achieve is the the following, instead of

product 1 | category1,category2|
product 2 | category2|

product 1 | category1,category2|
product 2 | category2|
i want the following

product 1 | category1|
product 1 | category2|
product 2 | category2|

product 1 | category1|
product 1 | category2|
product 2 | category2|
so it's just a matter of displaying what i tried to use is to create a custom column and call getrecord to add them manually, it's a workaround that's messy so i just discarded it in the end, but i wonder if there's an easy way to acheive it
Dan Harrin
Dan Harrin2y ago
sounds like you want a belongstomany relationship? with a relation manager? you shouldnt put duplicate rows in your table
Cybrarist
CybraristOP2y ago
i already have many to many relationship and the relationship is displayed like above. no issues with that usually i shouldn't, but what if i want to following product 1 | Store 1 | price in store 1 | _ product 1 | store 2 | price in store 2 | otherwise, the above will show llike this product 1 | store 1 , store 2 | price store 1 , price store 2 in the end i ended up doing the following just to make it better

Tables\Columns\TextColumn ::make('stores.name')->formatStateUsing(function ($state){
return Str::of(Str::replace("," , "<br>" , $state))->toHtmlString() ;
}),

Tables\Columns\TextColumn ::make('stores.name')->formatStateUsing(function ($state){
return Str::of(Str::replace("," , "<br>" , $state))->toHtmlString() ;
}),
then i would get product 1 | store 1 | price 1 | store 2 | price 2 ---------------------------- but ofc it would've been better to duplicate the row
Dan Harrin
Dan Harrin2y ago
right but this isnt a products table anymore this is a productStore table each row is a ProductStore pivot model
Cybrarist
CybraristOP2y ago
true, i didn't try pivot model method, let me check if it works so i did the following which didn't work
public function stores()
{
return $this->belongsToMany(Store::class)->using(ProductStore::class)->withTimestamps()->withPivot(['price'])
}
public function stores()
{
return $this->belongsToMany(Store::class)->using(ProductStore::class)->withTimestamps()->withPivot(['price'])
}
also i tried the following which also didn't work
public function product_stores()
{
return $this->hasMany(ProductStore::class);
}
public function product_stores()
{
return $this->hasMany(ProductStore::class);
}
with Tables\Columns\TextColumn ::make('product_services.id') not sure if im missing something, but in both cases they show record1,record2 in the same row
Kenneth Sese
Kenneth Sese2y ago
I think what Dan is saying is that you need another/different table. You're basically trying to add a bunch of mini ProductStore tables inside your Products table which isn't currently possible. One solution would be to add a ProductStoreRelationManager to your Products Resource and then when you click to view a Product you'd see the relation manager and the product's stores and prices. The other solution is just change the resource to reference your ProductStore pivot model. The only issue here is you'd see product's name duplicated for each store. And if you sorted the table they wouldn't be grouped anymore. If you need to see the stores/prices in that table then you'd have to do your own custom column like you were doing right now. But I believe that grouping is coming in v3.
Want results from more Discord servers?
Add your server