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
I found this on the documentation but it is not working or I cannot manage to do that
Put
->visible(fn ($record) => your_condition)
on the actions. Check the docs for Closure Customisation and ActionsThank 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?why not use a model policy?
that provides you with authorization too
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.
Policies are a Laravel feature, so check the Laravel docs π
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.
You can just edit the policies (just make sure you don't regenerate via Shield)
Okay, make sense. Thank you much.
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
so
->hidden(fn ($record) => ...)
?