bastinald/laravel-livewire-model

Laravel Livewire form data modelling trait.

Downloads

56

Stars

1

Version

2.0.2

Laravel Livewire Model

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.

Documentation

Installation

Require the package via composer:

composer require bastinald/laravel-livewire-model

Usage

The WithModel Trait

Add the WithModel trait to your Livewire components:

class Login extends Component
{
    use WithModel;
    
    //
}

Getting Model Data

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']);

Setting Model Data

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());

Binding Model Data

Just prepend your wire:model attribute with model.:

<input 
    type="email" 
    placeholder="{{ __('Email') }}"
    class="form-control" 
    wire:model.defer="model.email"
>

Validating Model Data

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.

bastinald

Author

bastinald