Confused on how to handle sorting in Filament and Laravel
I am rewriting, a old application that reuire the sorting of record both for full table or in groups like categories and subcategories
so far i looked at this package : https://github.com/ninoman/laravel-sortable/tree/master
and this package from Filament: https://filamentphp.com/plugins/ibrahim-bougaoua-sort-order
GitHub
GitHub - ninoman/laravel-sortable: Simple trait to add sorting to y...
Simple trait to add sorting to your Laravel models. - ninoman/laravel-sortable
Filament
Sort Order by Ibrahim Bougaoua - Filament
Transform the sorting order of any table effortlessly.
57 Replies
What I normally do is: in my AppServiceProvider
And on the tables that I don't want reordering I just put:
This way I just need to make a migration to add the column on the tables
there is a reorderable fuction in laravel?
not laravel, but on filament... You're using filament right?
yeah
does the order get reflected in the frontend too?
That Table class is from Filaments Table package. You can configure some things globally
Yes, of course. If you can control the query you can sort it by the column
it that case the column is named 'sort' so every resource model that has that column it will be returned to the table sorted
Reordering records
To allow the user to reorder records using drag and drop in your table, you can use the $table->reorderable() method:
use Filament\Tables\Table;
public function table(Table $table): Table
{
return $table
->reorderable('sort');
}
If you're using mass assignment protection on your model, you will also need to add the sort attribute to the $fillable array there.
When making the table reorderable, a new button will be available on the table to toggle reordering.
ah found it
the docs are so big
if not, you can add it to the config too, like so:
i did not even arrive at that part
ahaha, happens to everyone xD
ah this is nice
i'll change the migration too
beware that the column doesnt update automatically on creation
now the only thing i have left is that grouping sort
i can't make an action?
I usually do this, but I'm still figuring out a way to make it automatically for every model. Maybe an observer or wtv
isn't it better to use max?
to be sure the order index is the highest?
in case the system failed and there is a gap
to me no, because if I have 5 rows on the table and I create a new row, I want the column sort of the new row to be 6 (5 +1)
I don't want to have 6 rows with sort values jumping wildly like: 1,4,56,69,420,2, etc etc
if I have 5 rows the sort should be from 1 to 5
but if you delete like record 2
you have 2 5
Now of course this depends on the code base
if you have changes often i'll use max
cause if you have 20
true, but till this point that issue never happened xD but when it happens I'll need to solve it
i'll be 20+1
me too i'm just presolving a problem
the codebase i'm rewriting
I know that feeling so well
has a lot of these sorting
by request of course
but the long indicate that no sorting has been done for 7 years
😂
is it like a reporting dashboard? My last phase of the project I'm in will be something like that
reports reports and lot of summaries, groups, etc etc
yeah sort of
now that you mentioned that issue I'm gonna look at these packages to see how they handled it
it's musical documenting thing
the filkament one
you can ignore is reordering by index
yeah but is using max to like you said
but the code seems interestign though
GitHub
filament-sort-order/src/Traits/SortOrder.php at main · ibrahimBouga...
Transform the sorting order of any table effortlessly. - ibrahimBougaoua/filament-sort-order
yeah max on index
when you click the buttons
is a bad idea
well the coes is small enough
to see how you cna change the framework
a bit
i think it was for an older version
i mean filamnent has drag and drop now apparently
i'm quite amazed about that
finally free from nova
or having to write my own admin
as a hobbyist it was a bit much
never tried nova
for laravel I only tried backpack before finding filament
i blame my own ignorance
imagine using nova for a project you don0t get paid
Do gaps in the order actually matter though.
they matter to me
cause of my second orderign problem
has many group ordering
So, what happens when you reorder an item higher, you would have to loop through every record and adjust their order number. That’s not going to be very performant.
lke subcategories parent 1 1 sub 2 sub 3 sub oarent 2 1sub 2sub ecc
yeah
normal orderign by substitution shuld be enough
The subcategories should have their own respective ordering.
yeah thats why I like my approach, it's not perfect but the numbers arent public, so if the client deletes the row 4, he doesnt know that the sort jumps from 3 to 5 xD
I agree. Only the db will know or care about the ordering. So just let it do what it does. If the query is correct the order will always be correct.
Only exceptions there would be if there are duplicate order numbers but that’s expected since it’s just how ordering works. Ties exist in ordering.
when you're grouping you can change the ordering query if I'm not mistaken
if he doesn't add a row
True, my fallback ordering is the created_at field
Totally valid and makes sense.
ah yeah in that case it's not a problem
i don't see it in the documentation
Customizing the Eloquent query ordering behavior
ah maybe it's this one
yeah that
but is it affected by reordeable?
only one way to find out
yeah i'll try in the morning on the demo app
but since it changes the rows, I would say yes (wont change the table value) but when you're clicking the actions it will render the table with that changes
either way if you want to force it to maintain the sort order you can add it to the query after the grouping order just in case, dunno if it works
ah no i only need to efrce the grouping
and make sure the ordering is only within the group
i need to go to sleep
Same xD
@Coolman thank you for the help
No worries! The important thing is to make it work.
i'll write here how it goes