Skip to content

Latest commit

 

History

History
121 lines (79 loc) · 2.17 KB

traits.md

File metadata and controls

121 lines (79 loc) · 2.17 KB

Documentation > Traits

Traits

This package provides a couple of handy traits.

WithModel

This trait makes form data model manipulation a breeze. No more having to create a component property for every single form input.

Getting Model Data

Get all values as an array:

$this->getModel();

Get a single value:

$this->getModel('name');

Get an array of specific values:

$this->getModel(['name', 'email']);

Passing an array to this method will always return an array, even if it is only one item.

Get an array of all values except certain ones:

$this->getModelExcept(['password', 'password_confirmation']);

Setting Model Data

Set a single value:

$this->setModel('name', 'Kevin');

Set multiple values:

$this->setModel([
    'name' => 'Kevin',
    'email' => '[email protected]',
]);

Working With Arrays

Add an empty array item:

$this->addModelItem('locations');

Remove an array item by its key:

$this->removeModelItem('locations', 3);

Order an array item by its key & direction:

$this->orderModelItem('locations', 3, 'up');

The direction should be up or down.

Binding Model Data

Package Blade components work with this trait via the model attribute:

<x-ux::input :label="__('Name')" model="name"/>
<x-ux::input :label="__('Email')" type="email" 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.

HasHashes

This trait will auto-hash model attributes when saving to the database.

Declare attributes to auto-hash via the hashes property:

// Illuminate\Database\Eloquent\Model
// Bastinald\Ux\Traits\HasHashes

class User extends Model
{
    use HasHashes;

    protected $hashes = ['password'];
}

This will only hash attributes that are not already hashed, so it will not slow down seeders.