Disable field if a relation manager has records

Let's say I've an order with lines and I want a select field that's in the header to be disabled if the order already has lines. I can just add a:
Select::make('field')
->disabled(fn (Order $order) => $order->lines()->count())
Select::make('field')
->disabled(fn (Order $order) => $order->lines()->count())
... and it will work but only when the page is entirely refreshed. It won't behave live as lines are added or removed. How can I achieve that dynamism? Maybe with events?
4 Replies
Hasnayeen
Hasnayeen15mo ago
have you tried with live or reactive method?
carloscapote
carloscapoteOP15mo ago
Yeap, but didn't work. Also tried dispatching events in the relation manager actions that triggered a fillForm when records were modified (added, changed, or deleted) but it didn't work either
DariusIII
DariusIII15mo ago
Have you tried something like this? ->disabled(fn (Get $get) => count($get('order')) > 0 Or something similar.
carloscapote
carloscapoteOP15mo ago
The issue is that I can't manage to make the disabled closure be executed when lines are changed. I guess there must be a way to execute it with events but I haven't been able to guess how yet Ok, nevermind. I wasn't properly dispatching the event in the action. Here's a summary on how to make it work: 1. you don't need to make the select reactive nor live, 2. in the after method of your actions in the relation manager, you have to dispatch an event
Tables\Actions\CreateAction::make()
->after(function ($livewire) {
$livewire->dispatch('orderLineCreated');
}),
Tables\Actions\CreateAction::make()
->after(function ($livewire) {
$livewire->dispatch('orderLineCreated');
}),
3. in the EditOrder page, you have to listen the event and call fillForm so the disabled method is executed:
class EditOrder extends EditRecord
{
protected $listeners = [
'orderLineCreated' => 'refreshForm',
'orderLineEdited' => 'refreshForm',
'orderLineDeleted' => 'refreshForm',
];

public function refreshForm()
{
$this->fillForm();
}
...
class EditOrder extends EditRecord
{
protected $listeners = [
'orderLineCreated' => 'refreshForm',
'orderLineEdited' => 'refreshForm',
'orderLineDeleted' => 'refreshForm',
];

public function refreshForm()
{
$this->fillForm();
}
...
Want results from more Discord servers?
Add your server