TiagoSilvaPereira / vemto-filament-plugin

A Vemto plugin for generating Filament admin panels
21 stars 9 forks source link

The filament table always sorts by created_at first before sorting on sortable column. #11

Closed tkerns closed 11 months ago

tkerns commented 1 year ago

One problem I have had recently is when trying to make a column sortable, or applying a default sort. It seems to always sort by created_at, then the specified sort column, so sorting never works.

I tracked it down to this trait applied to the list page.

use App\Filament\Traits\HasDescendingOrder;

I don't remember this trait previously. Is there something I did in Vemto to trigger that? I am just wondering of what use it is, as it is causing sorting problems for me.

TiagoSilvaPereira commented 1 year ago

Adding @nicollassilva to the conversation

nicollassilva commented 1 year ago

Hi @tkerns , I hope you are well.

Using the User model to exemplify the explanation.

The HasDescendingOrder class is a trait used by Filament's ListUsers class, located at:

App/Filament/Resources/UserResource/Pages/ListUsers.php.

This trait is for items to be shown in the Filament listing in descending order, based on the model's created_at. By default, whenever you add an item to the table, Filament will read it in ascending order, so your item would be on the last page, as the last item to be shown. This trait is for him to reorder the results bringing from the last to the first.

I did some tests with the sortable() method and it worked perfectly, I didn't have sorting problems even using the trait. If you just want to disable the trait for the feature you are trying to reorder, you can follow the file path it uses (described above - remember to consider the template name by the way) and delete it inside the class. Remembering that you will lose the possibility of displaying the listings in descending order.

But if you want to share your .vemto project so I can look at it in detail, you can leave the link below and I'll analyze it for you.