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?
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
Then cache?
also thought about that, no luck
š«
What did you get in subscriptions and subscriptionYear
what do you mean exactly?
what kind of data is return when you call subscription or subsYear
what's the variable I have to do the dd() for (or use laravel ray, I also have that)?
that stores the incoming data
$record..
where should I put the dd()? the place where I put it is only showing me the properties of the first instance of the resource model (subject, which doesn't have the subscriptionYears)
I usually put inside like the link i sent, maybe filament will not load the relationships, so you can manually doing by modifyQueryUsing() in the $table for checking
You can do (Model $record, $state) to get the data and dd('$record')
placing it into formatStateUsing() worked!!! placing it into other places (like modifyQueryUsing() wasn't working)
even in the dd(), they're not ordered
what I get is the first instance of the SubjectResource table, with the subscriptions relation and each istance of the subscriptions. In each subscription instance, I have the subscriptionYear relation.
modifyQueryUsing() is meant for loading realtionship just in case š
You need to debug each line š