Order TextColumn relationship results by desc
this might be an easy thing but I can't find a way to do it š
as shown in the screenshot, I have a TextColumn that loads results from a relationship. So there might be multiple years. I would like to order them by desc, so that the latest year appears as first. How do I do that?
![No description](https://cdn.answeroverflow.com/1259825653818200064/sortbydesc.png)
26 Replies
Easiest way will be
orderBy()
in relationship itself imootherwise you need to do like this https://filamentphp.com/docs/3.x/tables/columns/text#custom-formatting and return order data
like this? I did try that but it doesn't work
I will look into the other thing you recommended me, thank you!
yeah this is what i have in mind, don't know why it not works š
I did try this but it seems it only works on the single instance, there isn't a way to work on all of them (the array of instances)
Hi,
Maybe by customizing the query?
"Method Filament\Tables\Columns\TextColumn::modifyQueryUsing does not exist."
I think that modifyQueryUsing can only be used as a parameter when you call the relationship() method, but TextColumn doesn't have that option
Not in the textColumn!
Read again, the first line:
$table
Oh, forget it, you don't want to reorder the table, my bad...Im on mobile rn, cannot check the code.. but what if you exclude .year in make()? By default filament displays all, so it should give arrays in somehow
I just tried VP's solution and it works: you can add an
orderBy()
in your BelongsToMany()
relation.
Can you show your Resource code (especially the relation)?yes, I really don't understand why it's not working š
even debugbar is showing the correct query, but the results are not actually ordered:
this is the relationship, in Subscription.php:
So, try to call only the relation and the field, like this:
Tables\Columns\TextColumn::make('subscriptionYear.year')
forgot to mention, I'm in a resource called subject, which has an HasMany relationship called subscriptions, which has a BelongsTo relationship called subscriptionYear
that's why I'm calling both
no luck with that either
only thing I can think of is that it is not working cos it's a nested relationship
Could you share the
subscriptions
relationship?
Did you try ->orderByDesc('year')
in the subscription relationship?in Subject.php
just tried what you suggested and the result is the same, it doesn't work even if debugbar shows the correct query
in Subscription.php