morningtrain / laravel-fields
API Fields
Installs: 2 856
Dependents: 2
Suggesters: 0
Security: 0
Stars: 0
Watchers: 4
Forks: 0
Open Issues: 0
Requires
- php: ^7.2|^8.0
- illuminate/database: ^5.8|^6.0|^7.0|^8.0|^9.0|^10.0
- illuminate/http: ^5.8|^6.0|^7.0|^8.0|^9.0|^10.0
- illuminate/support: ^5.8|^6.0|^7.0|^8.0|^9.0|^10.0
- illuminate/validation: ^5.8|^6.0|^7.0|^8.0|^9.0|^10.0
- morningtrain/laravel-support: ^1.0
This package is auto-updated.
Last update: 2024-10-08 08:13:26 UTC
README
Install
Via Composer
$ composer require morningtrain/laravel-fields
Features
A field is at its core responsible for taking a request value and applying it to an eloquent model attribute.
Applying fields
Having a field and a model instance, it can be applied like this:
$field->update($model, $request, FieldContract::BEFORE_SAVE);
and the following for post-save.
$field->update($model, $request, FieldContract::AFTER_SAVE);
Some fields will know that it should run before saving the model (like setting a basic attribute value) while some fields should be run after (attaching related models).
A more complete example would be:
$request = request(); if (is_array($fields) && !empty($fields)) { foreach ($fields as $field) { $field->update($model, $request, FieldContract::BEFORE_SAVE); } $model->save(); foreach ($fields as $field) { $field->update($model, $request, FieldContract::AFTER_SAVE); } }
The example uses the following classes:
use Illuminate\Database\Eloquent\Model; use MorningTrain\Laravel\Fields\Contracts\FieldContract;
Fields
Basic field
Field::create('name_of_attribute')
Hidden (static) field
Field::hidden('name_of_attribute', 'value to apply')
Belongs to many field
Field::belongsToMany('name_in_request') ->relation('name_of_relation') ->removeMissing()
Credits
This package is developed and actively maintained by Morningtrain.
_- _ -__ - -- _ _ - --- __ ----- _ --_
( Morningtrain, Denmark )
`---__- --__ _ --- _ -- ___ - - _ --_ ยด
o
. ____
_||__| | ______ ______ ______
( | | | | | | |
/-()---() ~ ()--() ~ ()--() ~ ()--()
--------------------------------------