What is a proper way to have sorted list together with reorderable action?

I want to display sorted table by order_to_display column and also have a possibility to reorder table items. The problem is that during reorder action orderBy gets duplicated, e.g. where `categories`.`menu_id` = 1 order by `products`.`order_to_display` asc, `products`.`order_to_display` asc, . What is a proper way and how to prevent duplicate of orderBy?
public static function table(Table $table): Table
{
......
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\RestoreAction::make(),
Tables\Actions\DeleteAction::make(),
])
->reorderable('products.order_to_display');
}

public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()
->orderBy('products.order_to_display');
}
public static function table(Table $table): Table
{
......
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\RestoreAction::make(),
Tables\Actions\DeleteAction::make(),
])
->reorderable('products.order_to_display');
}

public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()
->orderBy('products.order_to_display');
}
19 Replies
Dennis Koch
Dennis Koch2y ago
Use ->defaultSort() instead of setting it inside getEloquentQuery()
Crylar
CrylarOP2y ago
Yeh this works. One more related question. I have the following table query with a join and both categories and products tables have order_to_display column.
protected function getTableQuery(): Builder
{
return parent::getTableQuery()
->when($this->menu,
fn (Builder $builder) => $builder
->join('categories', 'categories.id', 'products.category_id')
->where('categories.menu_id', $this->menu)
->select([
...Arr::prependValuesWith([
'id',
'title',
'image',
'price',
'status'
], 'products.'),

'categories.title as category_title'
])
);
}
protected function getTableQuery(): Builder
{
return parent::getTableQuery()
->when($this->menu,
fn (Builder $builder) => $builder
->join('categories', 'categories.id', 'products.category_id')
->where('categories.menu_id', $this->menu)
->select([
...Arr::prependValuesWith([
'id',
'title',
'image',
'price',
'status'
], 'products.'),

'categories.title as category_title'
])
);
}
So in order to overcome ambiguous column error when starting reorder I need to define my reorder column as this: ->reorderable('products.order_to_display') , reorder loads but when I try to reorder items I get Add [products.order_to_display] to fillable property to allow mass assignment on [App\Models\Product]. but Model has protected $guarded = [];. I guess update field should not be with a table prefix. Is there a solution to this? @Dennis Koch
Dennis Koch
Dennis Koch2y ago
Do you need both order_to_display? Select one or alias the other one.
Crylar
CrylarOP2y ago
In a query, yes I need to prefix with a table, otherwise MySQL does not know if I want to order by column found in products or categories table because of JOIN.
Dennis Koch
Dennis Koch2y ago
You can still add an alias on ->select() right?
Crylar
CrylarOP2y ago
->select([ ...Arr::prependValuesWith([ 'id', 'title', 'image', 'price', 'status' ], 'products.'), 'categories.title as category_title' ]) yes I am removing any of columns completely
Dennis Koch
Dennis Koch2y ago
Hm, if that doesn't work, I am not sure. You could try overwriting the update logic when reordered
Crylar
CrylarOP2y ago
I mean the display for both table, and reordeable action table works. The problem comes when I reorder items and save to the database.
Crylar
CrylarOP2y ago
Crylar
CrylarOP2y ago
$orderColumn becomes "products.order_to_display" and I guess eloquent just does not like dot notation here.
Dennis Koch
Dennis Koch2y ago
Yeah, I understand. That's why I said: Maybe you need to overwrite that reorderTable() logic
Crylar
CrylarOP2y ago
Oh I can do this from the ListRecords :} Thanks I will try. 🙂
Crylar
CrylarOP2y ago
@Dennis Koch do you know when this part gets triggered? I could not find anything in the documentation yet. I wonder if we could just trim prefixes with dot and take any string after dot.
Dennis Koch
Dennis Koch2y ago
Seems like that's used for columns on a relation.
Crylar
CrylarOP2y ago
Yeh, just was wondering if dot notation plays in here. I do not see this in a code, so maybe not.
ba_mbi_07
ba_mbi_072y ago
Does reordering column make column 's value swap or make changes that whole column values
Dan Harrin
Dan Harrin2y ago
all the values change
ba_mbi_07
ba_mbi_072y ago
Does filament have something like we can swap two values in list record not whole column values
Dan Harrin
Dan Harrin2y ago
i dont think so
Want results from more Discord servers?
Add your server