Documentation > Traits
This package provides a couple of handy traits.
This trait makes form data model manipulation a breeze. No more having to create a component property for every single form input.
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']);
Set a single value:
$this->setModel('name', 'Kevin');
Set multiple values:
$this->setModel([
'name' => 'Kevin',
'email' => '[email protected]',
]);
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
.
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"/>
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.
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.