In this article, we are exploring 5 filament tips and tricks that may come handy for you when working on this admin panel.
Clickable action on table row.
In filament table, whenever we click on any record on the table, it redirects us to edit page of that resource. So, if you want to change that, you can use the following code.
public function table(Table $table): Table
{
return $table
->recordUrl(
fn (Model $record): string => ProductResource::getUrl('edit',['record'=>$record->id])
)
}
In the above code, the recordUrl method determines the action or redirection for the particular record.
Clickable action on particular column
Lets take an example, I have a product table and product belongs to Category. So, in my table i am listing the Category Name but what i want is whenever user clicks on that category name, use should redirect to the category page.
So, for that , lets use the following code.
Tables\Columns\TextColumn::make('category.name')
->label(__("Category"))
->searchable()
->sortable()
->url(fn (Model $record): string => CategoryResource::getUrl('edit',['record'=>$record->category_id])),
If you see the code above, we have chained the url() method that can be used to redirect when clicked.
Read More From Us:
- How to add Language switcher in filament?
- How to Send Laravel Logs to Discord (Step-by-Step Guide)
- How to add Tabs on custom filament page?
- Mastering Enums in Laravel Filament: A Complete Guide with Practical Examples
- Mastering Enums in Laravel: A Complete Guide with Practical Examples
Redirect users from custom action
Actions and Url are for different reasons. But using Action method also we can modify users to different routes. So lets take an example.
Action::make('print')
->label('Print')
->icon('heroicon-o-printer')
->form([
Forms\Components\Section::make()
->columns(2)
->schema([
Forms\Components\DatePicker::make('start_date')
->label('Start Date')
->required(),
Forms\Components\DatePicker::make('end_date')
->label('End Date')
->required(),
]),
])
->action(fn (array $data) => redirect()->route('print-report', [
'tenant' => Filament::getTenant()->id,
'start_date' => $data['start_date'],
'end_date' => $data['end_date'],
));
If you check the above example, you can see i am using filters and redirecting users to print the records. So, I am using action and redirecting users to custom route.
Improve Performance of Dynamic Form Fields in Filament
If you have created a dynamic form field and others field are dependent on this, you must have felt a little bit slow response. So, to handle that we can use the following code:
Forms\Components\TextInput::make('amount')
->label(__('transaction.form.amount'))
->required()
->numeric()
->reactive()
->live(onBlur: true)
->afterStateUpdated(function (Set $set, $state, callable $get) {
if ($state) {
// some actions to perform.
}
}),
You can use live(onBlur:true) or debounce(time) to make it look more flexible and fast responsable.
Implement Table Row Reordering
In some cases, we need to have a functionality to reorder the table records. So we can do this easily. Before, we need have at least a column in database like sort, order that stores the order information. If you add that , then we can easily use it on the table() method:
......
return $table
->reorderable('sort')
......
Conclusion
These 5 filament tips and tricks are not advance tips and tricks but they are the basic ones that we need on most of our projects but its quite difficult to find them. So, If you have any confusion regarding the above, you can comment down below or directly email me.
Don’t forget to checkout the official docs. As docs is the king.
[…] Learning Filament, check out the official docs. Also check out the tips and tricks for filament. […]
[…] In this article, we are going to discuss essential laravel filament tips and tricks that every developer must know. In the last article, we discussed about 5 such filament tips and tricks. You can check that article here. […]