F
Filamentβ€’15mo ago
onursahindur

Conditional Table Row Actions

Hello, I want to achieve a row based action in a table in one resource. In the screenshot that I added I have a ContentTypeManagementResource which lists the content types basically. I have a relation in ContentType model as "corp_id" which can be nullable, since there exists predefined ContentTypes. I do not want some admin can delete these records, so for example I want to hide "edit" and "delete" action for those records, but show them if a record has corp_id How can I achieve this?
12 Replies
onursahindur
onursahindurβ€’15mo ago
onursahindur
onursahindurβ€’15mo ago
I found this on the documentation but it is not working or I cannot manage to do that
Dennis Koch
Dennis Kochβ€’15mo ago
Put ->visible(fn ($record) => your_condition) on the actions. Check the docs for Closure Customisation and Actions
onursahindur
onursahindurβ€’15mo ago
Thank you, Before see your message I also found, May be help any other when searching in discord. Tables\Actions\ViewAction::make() ->disabled(function (ContentType $record) { error_log($record); return $record->corp_id === null; }), Can I also ask another question to you? How can I prevent editing with URL? Is there a way to pop back when corp_id is null which means predefined? http://localhost/panel/icerik-turleri/6/edit for example this, I want to prevent this page editing when some record is not editable Okay, I think I can achieve it by overriding the mount function from EditRecord. For Example; public function mount($record): void { parent::mount($record); error_log($this->previousUrl); error_log($this->getRecord()); if ($this->getRecord()->corp_id === null) { // POP BACK HERE } } Is this a correct way to achieve it?
Dan Harrin
Dan Harrinβ€’15mo ago
why not use a model policy? that provides you with authorization too
onursahindur
onursahindurβ€’15mo ago
I am new to your community and still in learning process πŸ™‚ We loved your work btw, thanks for making it better. Let me check the documentation how can we apply a model policy. Thanks.
Dennis Koch
Dennis Kochβ€’15mo ago
Policies are a Laravel feature, so check the Laravel docs πŸ˜‰
onursahindur
onursahindurβ€’15mo ago
Yes, I noticed that πŸ˜… I am using filament/shield repository, so it has a policy defined already. Now I am searching how can I achieve both this repo and a custom policy to it πŸ™‚ I hope i will find a good way to achieve it.
Dennis Koch
Dennis Kochβ€’15mo ago
You can just edit the policies (just make sure you don't regenerate via Shield)
onursahindur
onursahindurβ€’15mo ago
Okay, make sense. Thank you much.
ba_mbi_07
ba_mbi_07β€’14mo ago
protected function getTableQuery(): EloquentBuilder
{
$categoryHandlers = parent::getTableQuery()->orderBy('position', 'asc');

$categories = WpTerm::query()
->whereHas('taxonomy', function ($taxonomy) {
$taxonomy->where('taxonomy', '=', 'category')
->where('parent', '=', 0);
})
->whereDoesntHave('categoryHandler')
->orderBy('name', 'asc')
->select(['name', 'term_id'])
->addSelect(DB::raw('null as position')); // Add a `position` column with null value

return $categoryHandlers->union($categories);

}
protected function getTableQuery(): EloquentBuilder
{
$categoryHandlers = parent::getTableQuery()->orderBy('position', 'asc');

$categories = WpTerm::query()
->whereHas('taxonomy', function ($taxonomy) {
$taxonomy->where('taxonomy', '=', 'category')
->where('parent', '=', 0);
})
->whereDoesntHave('categoryHandler')
->orderBy('name', 'asc')
->select(['name', 'term_id'])
->addSelect(DB::raw('null as position')); // Add a `position` column with null value

return $categoryHandlers->union($categories);

}
this is how i am showing my table and i wanted to add action on those who are not in my record is there any way i can add condition on my action and get the row data
Tables\Actions\Action::make('activate')
->label('activate')
Tables\Actions\Action::make('activate')
->label('activate')
Dan Harrin
Dan Harrinβ€’14mo ago
so ->hidden(fn ($record) => ...) ?