trexology / extendable
Traits for Laravel to add and manage custom Eloquent model fields.
1.5.3
2019-03-13 07:30 UTC
Requires
- php: >=5.3.0
Requires (Dev)
- phpunit/phpunit: ~4.0
README
How to install
Composer Install
composer require trexology/extendable
Laravel Service Provider (not required for laravel version above 5.5)
Add service provider in app/config/app.php
'providers' => [ Trexology\Extendable\Providers\ExtendableServiceProvider::class, ];
Publish configs, templates and run migrations.
php artisan vendor:publish --provider="Trexology\Extendable\Providers\ExtendableServiceProvider" php artisan migrate
Usage
Add traits
Add model trait to models, where you want to use custom fields.
use Trexology\Extendable\Traits\Extendable; class Article extends \Illuminate\Database\Eloquent\Model { use Extendable; }
Config fields
Use app/config/custom-fields.php
to configure your fields.
return [ 'App\User' => [ // model name 'gender' => [ // field name 'title' => 'Gender', // field title (can be used in views) 'type' => \Trexology\Extendable\CustomFieldType::Radio, // field type 'options' => [ // possible values/labels 'male' => 'Male', 'female' => 'Female' ], 'default' => 'male' // default value ] ] ];
Assign/retrieve customfield values
Assign custom field values as regular values.
$data = [ 'title' => 'Awesome Article!!!', // regular field 'gender' => 'male' // custom filed ]; $article = new Article(); $article->fill($data); $article->save();
Retrieve custom field values.
$article = Article::find(1); $article->gender->value; // 1 echo $article->gender; // 1 $article->customFields; // return collection of custom fields