F
Filamentā€¢5mo ago
zemo

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?
Tables\Columns\TextColumn::make('subscriptions.subscriptionYear.year')
->label('Iscrizioni')
->searchable()
->listWithLineBreaks()
->limitList(1)
->expandableLimitedList(),
Tables\Columns\TextColumn::make('subscriptions.subscriptionYear.year')
->label('Iscrizioni')
->searchable()
->listWithLineBreaks()
->limitList(1)
->expandableLimitedList(),
No description
26 Replies
Vp
Vpā€¢5mo ago
Easiest way will be orderBy() in relationship itself imo
zemo
zemoOPā€¢5mo ago
like this? I did try that but it doesn't work
public function subscriptionYear(): BelongsTo
{
return $this->belongsTo(SubscriptionYear::class)->orderBy('year', 'desc');
}
public function subscriptionYear(): BelongsTo
{
return $this->belongsTo(SubscriptionYear::class)->orderBy('year', 'desc');
}
I will look into the other thing you recommended me, thank you!
Vp
Vpā€¢5mo ago
yeah this is what i have in mind, don't know why it not works šŸ˜†
zemo
zemoOPā€¢5mo ago
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)
micraux
micrauxā€¢5mo ago
Hi, Maybe by customizing the query?
$table
->modifyQueryUsing(function (Builder $query) {
$query->orderBy(...)
})
$table
->modifyQueryUsing(function (Builder $query) {
$query->orderBy(...)
})
zemo
zemoOPā€¢5mo ago
"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
micraux
micrauxā€¢5mo ago
Not in the textColumn! Read again, the first line: $table Oh, forget it, you don't want to reorder the table, my bad...
Vp
Vpā€¢5mo ago
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
micraux
micrauxā€¢5mo ago
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)?
zemo
zemoOPā€¢5mo ago
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:
select * from `subscription_years` where `subscription_years`.`id` in (1, 2, 6) order by `year` desc
select * from `subscription_years` where `subscription_years`.`id` in (1, 2, 6) order by `year` desc
this is the relationship, in Subscription.php:
public function subscriptionYear(): BelongsTo
{
return $this->belongsTo(SubscriptionYear::class)->orderBy('year', 'desc');
}
public function subscriptionYear(): BelongsTo
{
return $this->belongsTo(SubscriptionYear::class)->orderBy('year', 'desc');
}
micraux
micrauxā€¢5mo ago
So, try to call only the relation and the field, like this: Tables\Columns\TextColumn::make('subscriptionYear.year')
zemo
zemoOPā€¢5mo ago
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
LeandroFerreira
LeandroFerreiraā€¢5mo ago
Could you share the subscriptions relationship? Did you try ->orderByDesc('year') in the subscription relationship?
zemo
zemoOPā€¢5mo ago
in Subject.php
public function subscriptions()
{
return $this->hasMany(Subscription::class);
}
public function subscriptions()
{
return $this->hasMany(Subscription::class);
}
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
public function subscriptionYear(): BelongsTo
{
return $this->belongsTo(SubscriptionYear::class)->orderByDesc('year');
}
public function subscriptionYear(): BelongsTo
{
return $this->belongsTo(SubscriptionYear::class)->orderByDesc('year');
}
Vp
Vpā€¢5mo ago
Then cache?
zemo
zemoOPā€¢5mo ago
also thought about that, no luck šŸ« 
Vp
Vpā€¢5mo ago
What did you get in subscriptions and subscriptionYear
zemo
zemoOPā€¢5mo ago
what do you mean exactly?
Vp
Vpā€¢5mo ago
what kind of data is return when you call subscription or subsYear
zemo
zemoOPā€¢5mo ago
what's the variable I have to do the dd() for (or use laravel ray, I also have that)? that stores the incoming data
Vp
Vpā€¢5mo ago
$record..
zemo
zemoOPā€¢5mo ago
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)
Vp
Vpā€¢5mo ago
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')
zemo
zemoOPā€¢5mo ago
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.
Vp
Vpā€¢5mo ago
modifyQueryUsing() is meant for loading realtionship just in case šŸ™‚ You need to debug each line šŸ˜…
Want results from more Discord servers?
Add your server