Repeater: show delete button on each line conditionally

I am trying to let the user only delete repeater records with certain data conditions. Currently I only managed to stop the delete action but I think there must be a way where I can even conditionally show/hide the delete button based on the data within the record.
Repeater::make('Links')
->relationship('weblinks')
->schema([
TextInput::make('url')
->url()
->disabled(fn (Get $get) => !Weblink::typeEditable($get('type'))) //readonly on some types
->dehydrated(),
Select::make('type')
->options(Weblink::getSelectWeblinkType())
->disabled(fn (Get $get) => !Weblink::typeEditable($get('type')))
->dehydrated()
])
->addActionLabel('New Link')
->defaultItems(0)
->deleteAction(function (Action $action) {
return $action->before(function ($state, $arguments, $action) {
if (isset($state[$arguments["item"]])) {
$item_id = Arr::get($arguments, "item");
$item = Arr::get($state, $item_id);
$type = Arr::get($item, "type");
if (Weblink::typeEditable($type)) {
return $action->hidden(false);
} else {
Notification::make()
->title('Error')
->body("Type not allowed")
->status('danger')
->send();
return $action->cancel();
}
}
});
})
Repeater::make('Links')
->relationship('weblinks')
->schema([
TextInput::make('url')
->url()
->disabled(fn (Get $get) => !Weblink::typeEditable($get('type'))) //readonly on some types
->dehydrated(),
Select::make('type')
->options(Weblink::getSelectWeblinkType())
->disabled(fn (Get $get) => !Weblink::typeEditable($get('type')))
->dehydrated()
])
->addActionLabel('New Link')
->defaultItems(0)
->deleteAction(function (Action $action) {
return $action->before(function ($state, $arguments, $action) {
if (isset($state[$arguments["item"]])) {
$item_id = Arr::get($arguments, "item");
$item = Arr::get($state, $item_id);
$type = Arr::get($item, "type");
if (Weblink::typeEditable($type)) {
return $action->hidden(false);
} else {
Notification::make()
->title('Error')
->body("Type not allowed")
->status('danger')
->send();
return $action->cancel();
}
}
});
})
I tried to make a similar condition in function Repeater->deleteable(function (...)) but here I don't have access to the $arguments, so I cant say which element of the state I am currently processing. Still the current solution I showed above is working for me but it feels wrong. I think there is a much easier way, I am just not seeing it yet probably.
1 Reply
Augadh
Augadh4mo ago
Any solution for this ?