https://github.com/bastinald/laravel-livewire-model
Laravel Livewire form data modelling trait.
https://github.com/bastinald/laravel-livewire-model
Last synced: over 1 year ago
JSON representation
Laravel Livewire form data modelling trait.
- Host: GitHub
- URL: https://github.com/bastinald/laravel-livewire-model
- Owner: bastinald
- Created: 2021-08-31T03:08:05.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2021-09-11T03:09:09.000Z (almost 5 years ago)
- Last Synced: 2025-01-24T08:43:37.785Z (over 1 year ago)
- Language: PHP
- Size: 4.88 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# 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](#installation)
- [Usage](#usage)
- [The WithModel Trait](#the-withmodel-trait)
- [Getting Model Data](#getting-model-data)
- [Setting Model Data](#setting-model-data)
- [Binding Model Data](#binding-model-data)
- [Validating Model Data](#validating-model-data)
## Installation
Require the package via composer:
```console
composer require bastinald/laravel-livewire-model
```
## Usage
### The WithModel Trait
Add the `WithModel` trait to your Livewire components:
```php
class Login extends Component
{
use WithModel;
//
}
```
### Getting Model Data
Get all model data as an array:
```php
$array = $this->getModel();
```
Get a single value:
```php
$email = $this->getModel('email');
```
Get an array of specific values:
```php
$credentials = $this->getModel(['email', 'password']);
```
### Setting Model Data
Set a single value:
```php
$this->setModel('name', 'Kevin');
```
Set values using an array:
```php
$this->setModel([
'name' => 'Kevin',
'email' => 'kevin@example.com',
]);
```
Set values using Eloquent model data:
```php
$this->setModel(User::first());
```
### Binding Model Data
Just prepend your `wire:model` attribute with `model.`:
```html
```
### Validating Model Data
Use the `validateModel` method to validate model data:
```php
$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.