Laravel Livewire form data modelling trait.
bastinald/laravel-livewire-model is a Laravel package for laravel livewire form data modelling trait..
It currently has 1 GitHub stars and 61 downloads on Packagist (latest version 2.0.2).
Install it with composer require bastinald/laravel-livewire-model.
Discover more Laravel packages by bastinald
or browse all Laravel packages to compare alternatives.
Last updated
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-model
Add 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' => '[email protected]',
]);
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.