F
Filamentā€¢3mo 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ā€¢3mo ago
Easiest way will be orderBy() in relationship itself imo
zemo
zemoā€¢3mo 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ā€¢3mo ago
yeah this is what i have in mind, don't know why it not works šŸ˜†
zemo
zemoā€¢3mo 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ā€¢3mo ago
Hi, Maybe by customizing the query?
$table
->modifyQueryUsing(function (Builder $query) {
$query->orderBy(...)
})
$table
->modifyQueryUsing(function (Builder $query) {
$query->orderBy(...)
})
zemo
zemoā€¢3mo 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ā€¢3mo 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ā€¢3mo 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ā€¢3mo 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
zemoā€¢3mo 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ā€¢3mo ago
So, try to call only the relation and the field, like this: Tables\Columns\TextColumn::make('subscriptionYear.year')
zemo
zemoā€¢3mo 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ā€¢3mo ago
Could you share the subscriptions relationship? Did you try ->orderByDesc('year') in the subscription relationship?
zemo
zemoā€¢3mo 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');
}
Want results from more Discord servers?
Add your server