Is it possible with Laravel livewire to change a field in the database when leaving the input field?
2 Answers
You can use wire:model.lazy
to achieve an action when leaving (loosing focus) on an input field.
<div>
<input type="text" class="form-control" wire:model.lazy="billingRate">
</div>
class Rate extends Component
{
public $billingRate;
public function updatedBillingRate(){
dd($this->billingRate); // don't forget to use $this to access class property
// persist to database here
}
}
In the above example, whenever billingRate value changes, it will trigger the updatedBillingRate()
method.
Please checkout a similar thred on laracast here (https://laracasts.com/discuss/channels/livewire/livewire-how-to-action-the-value-of-an-input-without-a-button)
-
You can also do the same thing using updated hook. public function updated($name,$value). Commented Jan 6, 2021 at 6:10
-
The actual syntax for lazy loading is wire:model.lazy instead of wire:model:lazy Commented Jan 7, 2021 at 4:27
In my own perspective, you can implement JavaScript event listener when leaving the input field, an ajax request will fire-up to update the database.