This package contains a trait which makes Laravel Livewire form data model manipulation a breeze. No more having to create a Livewire component property for every single form input. All form data will be placed inside the $model property array.
Require the package via composer:
composer require bastinald/laravel-livewire-modelAdd the WithModel trait to your Livewire components:
class Login extends Component
{
use WithModel;
//
}Get all model data as an array:
$array = $this->getModel();Get a single value:
$email = $this->getModel('email');Get an array of specific values:
$credentials = $this->getModel(['email', 'password']);Set a single value:
$this->setModel('name', 'Kevin');Set values using an array:
$this->setModel([
'name' => 'Kevin',
'email' => 'kevin@example.com',
]);Set values using Eloquent model data:
$this->setModel(User::first());Just prepend your wire:model attribute with model.:
<input
type="email"
placeholder="{{ __('Email') }}"
class="form-control"
wire:model.defer="model.email"
>Use the validateModel method to validate model data:
$this->validateModel([
'name' => ['required'],
'email' => ['required', 'email'],
]);This method works just like the Livewire validate method, so you can specify your rules in a separate rules method if you prefer.