F
Filament3mo ago
Ermac

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
Bruno Pereira
Bruno Pereira3mo ago
What I normally do is: in my AppServiceProvider
public function boot(): void
{
Table::configureUsing(function (Table $table) {
$table->reorderable('sort');

});
}
public function boot(): void
{
Table::configureUsing(function (Table $table) {
$table->reorderable('sort');

});
}
And on the tables that I don't want reordering I just put:
return $table
->reorderable(false)
return $table
->reorderable(false)
This way I just need to make a migration to add the column on the tables
Ermac
ErmacOP3mo ago
there is a reorderable fuction in laravel?
Bruno Pereira
Bruno Pereira3mo ago
not laravel, but on filament... You're using filament right?
Ermac
ErmacOP3mo ago
yeah does the order get reflected in the frontend too?
Bruno Pereira
Bruno Pereira3mo ago
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
Ermac
ErmacOP3mo ago
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
Bruno Pereira
Bruno Pereira3mo ago
if not, you can add it to the config too, like so:
public function boot(): void
{
Table::configureUsing(function (Table $table) {
$table->reorderable('sort');
$table->modifyQueryUsing(function (Builder $query) use ($table) {
$query->orderBy("sort",'desc');
});

});
}
public function boot(): void
{
Table::configureUsing(function (Table $table) {
$table->reorderable('sort');
$table->modifyQueryUsing(function (Builder $query) use ($table) {
$query->orderBy("sort",'desc');
});

});
}
Ermac
ErmacOP3mo ago
i did not even arrive at that part
Bruno Pereira
Bruno Pereira3mo ago
ahaha, happens to everyone xD
Ermac
ErmacOP3mo ago
ah this is nice i'll change the migration too
Bruno Pereira
Bruno Pereira3mo ago
beware that the column doesnt update automatically on creation
Ermac
ErmacOP3mo ago
now the only thing i have left is that grouping sort i can't make an action?
Bruno Pereira
Bruno Pereira3mo ago
protected function handleRecordCreation(array $data): Model
{
$count = MyModel::count();
$data['sort'] = ++$count;
return static::getModel()::create($data);
}
protected function handleRecordCreation(array $data): Model
{
$count = MyModel::count();
$data['sort'] = ++$count;
return static::getModel()::create($data);
}
I usually do this, but I'm still figuring out a way to make it automatically for every model. Maybe an observer or wtv
Ermac
ErmacOP3mo ago
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
Bruno Pereira
Bruno Pereira3mo ago
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
Ermac
ErmacOP3mo ago
but if you delete like record 2 you have 2 5
Bruno Pereira
Bruno Pereira3mo ago
Now of course this depends on the code base
Ermac
ErmacOP3mo ago
if you have changes often i'll use max cause if you have 20
Bruno Pereira
Bruno Pereira3mo ago
true, but till this point that issue never happened xD but when it happens I'll need to solve it
Ermac
ErmacOP3mo ago
i'll be 20+1 me too i'm just presolving a problem the codebase i'm rewriting
Bruno Pereira
Bruno Pereira3mo ago
I know that feeling so well
Ermac
ErmacOP3mo ago
has a lot of these sorting by request of course but the long indicate that no sorting has been done for 7 years 😂
Bruno Pereira
Bruno Pereira3mo ago
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
Ermac
ErmacOP3mo ago
yeah sort of
Bruno Pereira
Bruno Pereira3mo ago
now that you mentioned that issue I'm gonna look at these packages to see how they handled it
Ermac
ErmacOP3mo ago
it's musical documenting thing the filkament one you can ignore is reordering by index
Bruno Pereira
Bruno Pereira3mo ago
yeah but is using max to like you said
Ermac
ErmacOP3mo ago
but the code seems interestign though
Ermac
ErmacOP3mo ago
yeah max on index
Bruno Pereira
Bruno Pereira3mo ago
when you click the buttons
Ermac
ErmacOP3mo ago
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
Bruno Pereira
Bruno Pereira3mo ago
never tried nova for laravel I only tried backpack before finding filament
Ermac
ErmacOP3mo ago
i blame my own ignorance imagine using nova for a project you don0t get paid
awcodes
awcodes3mo ago
Do gaps in the order actually matter though.
Ermac
ErmacOP3mo ago
they matter to me cause of my second orderign problem has many group ordering
awcodes
awcodes3mo ago
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.
Ermac
ErmacOP3mo ago
lke subcategories parent 1 1 sub 2 sub 3 sub oarent 2 1sub 2sub ecc yeah normal orderign by substitution shuld be enough
awcodes
awcodes3mo ago
The subcategories should have their own respective ordering.
Bruno Pereira
Bruno Pereira3mo ago
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
awcodes
awcodes3mo ago
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.
Bruno Pereira
Bruno Pereira3mo ago
when you're grouping you can change the ordering query if I'm not mistaken
Ermac
ErmacOP3mo ago
if he doesn't add a row
Bruno Pereira
Bruno Pereira3mo ago
True, my fallback ordering is the created_at field
awcodes
awcodes3mo ago
Totally valid and makes sense.
Ermac
ErmacOP3mo ago
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
Ermac
ErmacOP3mo ago
yeah that but is it affected by reordeable?
Bruno Pereira
Bruno Pereira3mo ago
only one way to find out
Ermac
ErmacOP3mo ago
yeah i'll try in the morning on the demo app
Bruno Pereira
Bruno Pereira3mo ago
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
Ermac
ErmacOP3mo ago
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
Bruno Pereira
Bruno Pereira3mo ago
Same xD
Ermac
ErmacOP3mo ago
@Coolman thank you for the help
Bruno Pereira
Bruno Pereira3mo ago
No worries! The important thing is to make it work.
Ermac
ErmacOP3mo ago
i'll write here how it goes
Want results from more Discord servers?
Add your server