#Confused on how to handle sorting in Filament and Laravel

102 messages · Page 1 of 1 (latest)

covert pewter
#

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

Simple trait to add sorting to your Laravel models. - ninoman/laravel-sortable

Filament

Transform the sorting order of any table effortlessly.

fallen hollowBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> ✅ Mark Solution

molten flame
#

What I normally do is: in my AppServiceProvider

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)

This way I just need to make a migration to add the column on the tables

covert pewter
#

there is a reorderable fuction in laravel?

molten flame
#

not laravel, but on filament... You're using filament right?

covert pewter
#

yeah

#

does the order get reflected in the frontend too?

molten flame
#

That Table class is from Filaments Table package. You can configure some things globally

molten flame
#

it that case the column is named 'sort' so every resource model that has that column it will be returned to the table sorted

covert pewter
#

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

molten flame
#

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');
            });

        });
    }
covert pewter
#

i did not even arrive at that part

molten flame
#

ahaha, happens to everyone xD

covert pewter
#

ah this is nice

#

i'll change the migration too

molten flame
#

beware that the column doesnt update automatically on creation

covert pewter
#

now the only thing i have left is that grouping sort

covert pewter
molten flame
#
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

covert pewter
#

to be sure the order index is the highest?

#

in case the system failed and there is a gap

molten flame
#

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

covert pewter
#

but if you delete like record 2

#

you have 2 5

molten flame
#

Now of course this depends on the code base

covert pewter
#

if you have changes often i'll use max

#

cause if you have 20

molten flame
#

true, but till this point that issue never happened xD but when it happens I'll need to solve it

covert pewter
#

i'll be 20+1

covert pewter
#

the codebase i'm rewriting

molten flame
#

I know that feeling so well

covert pewter
#

has a lot of these sorting

#

by request of course

#

but the long indicate that no sorting has been done for 7 years

#

😂

molten flame
#

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

covert pewter
#

yeah sort of

molten flame
#

now that you mentioned that issue I'm gonna look at these packages to see how they handled it

covert pewter
#

it's musical documenting thing

covert pewter
#

you can ignore is reordering by index

molten flame
#

yeah but is using max to like you said

covert pewter
#

but the code seems interestign though

molten flame
covert pewter
#

yeah max on index

molten flame
#

when you click the buttons

covert pewter
#

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

molten flame
#

never tried nova

#

for laravel I only tried backpack before finding filament

covert pewter
#

i blame my own ignorance

#

imagine using nova for a project you don0t get paid

river condor
#

Do gaps in the order actually matter though.

covert pewter
#

they matter to me

#

cause of my second orderign problem

#

has many group ordering

river condor
#

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.

covert pewter
#

lke subcategories parent 1 1 sub 2 sub 3 sub oarent 2 1sub 2sub ecc

covert pewter
#

normal orderign by substitution shuld be enough

river condor
#

The subcategories should have their own respective ordering.

molten flame
#

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

river condor
#

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.

molten flame
covert pewter
#

if he doesn't add a row

molten flame
river condor
covert pewter
#

ah yeah in that case it's not a problem

covert pewter
#

Customizing the Eloquent query ordering behavior

#

ah maybe it's this one

covert pewter
#

yeah that

#

but is it affected by reordeable?

molten flame
#

only one way to find out

covert pewter
#

yeah i'll try in the morning on the demo app

molten flame
#

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

covert pewter
#

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

molten flame
covert pewter
#

@molten flame thank you for the help

molten flame
#

No worries! The important thing is to make it work.