Summing TextColumn on nested relationship
I have a GroupOrderResource that has many UserOrder, with the relationship
userOrders
, then my UserOrder model has this relationship with ProductSku:
Then I want my TextColumn to show all ProductSku quantities for each Group Order, and my attempt was to do this:
But I get this error:
Is this an issue with attempting to sum a number from a nested relationship, or am I doing something else wrong?Solution:Jump to solution
After some tinkering, I managed to do what I wanted like this:
```php
TextColumn::make('userOrders.userOrderItems.quantity')
->label('Items')
->formatStateUsing(function ($state) {...
3 Replies
I don't think Filament handles such nested relationship. Even on Eloquent level I'm not sure you would be able to run a similar query with "userOrders.productSkus".
Maybe consider a hasManyThrough relation?
Hmm can I create a hasManyThrough relationship on the GroupOrder model with the ProductSku model, through the UserOrder model? It is important for my operations that there is a ManyToMany relationship between UserOrder and ProductSku, so ProductSku does not have a user_order_id attribute that I can use for the hasManyThrough relationship directly.
Solution
After some tinkering, I managed to do what I wanted like this: