tmsllc / laravel-extra-field
A package to enable assigning extra fields to Eloquent Models
Installs: 1 392
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^8.0
- illuminate/support: ^8.71|^9.0
Requires (Dev)
- orchestra/testbench: ^6.23|^7.0
- phpunit/phpunit: ^9.4
README
A package to enable assigning extra fields to Eloquent Models
Contact Me
You can check all of my information by Checking my website.
Installation
You can install the package via composer:
composer require tmsllc/laravel-extra-field
The package will automatically register itself.
You must publish the migration with:
php artisan vendor:publish --provider="TMSLLC\ExtraField\ExtraFieldServiceProvider" --tag="migrations"
Migrate the extras
& extra_values
table:
php artisan migrate
Optionally you can publish the config-file with:
php artisan vendor:publish --provider="TMSLLC\ExtraField\ExtraFieldServiceProvider" --tag="config"
This is the contents of the file which will be published at config/extra-field.php
return [ /* * The class name of the extra model that holds all extras. * * The model must be or extend `TMSLLC\ExtraField\Extra`. */ 'extra_model' => TMSLLC\ExtraField\Extra::class, /* * The class name of the extra value model that holds all values. * * The model must be or extend `TMSLLC\ExtraField\ExtraValue`. */ 'extra_value_model' => TMSLLC\ExtraField\ExtraValue::class, /* * The name of the column which holds the ID of the model related to the extra values. * * You can change this value if you have set a different name in the migration for the extra_values table. */ 'model_primary_key_attribute' => 'model_id', /* * The name of the column which holds the Class Name of the model related to the extras. * * You can change this value if you have set a different name in the migration for the extras table. */ 'model_name_attribute' => 'model_class', ];
Usage
Add the HasExtraFields
trait to a model you like to use extras on.
use TMSLLC\ExtraField\HasExtraFields; class YourEloquentModel extends Model { use HasExtraFields; }
Add a new extra field and value
You can add a new extra field like this:
$extra_field = $model->addExtraField('fieldName' , 'fieldType'); //assign value $extra_field = $model->addExtraValue($extra_field->id , 'filedValue'); //assign value for other instance no need to add extra field again $extra_field = $otherModel->addExtraValue($extra_field->id , 'filedValue'); //for another instance $extra_field = $anotherModel->addExtraValue($extra_field->id , 'filedValue');
Add a new extra field string type and assign value
if you want to add a new string extra field for your model you can do it like this:
$model->addStringExtraValue('fieldName' , 'filedValue'); //you can repeat for other object $otherModel->addExtraValue('fieldName', 'filedValue');
Retrieve data
// will give array of all extra fields with associated values $model->getExtras(); // ['fieldName1' => 'filedValue' , 'fieldName2' => 'filedValue'] $model->extras(); // will return a collection of TMSLLC\ExtraField\Extra $model->extraValues ; //will return a collection of TMSLLC\ExtraField\ExtraValue $model->extraValues() ; // will return hasMany Relation of TMSLLC\ExtraField\ExtraValue
Drop extra field
//this wil drop the value of the given name extra field for this model $model->dropExtraFieldData($name) ;
Drop extra field and all related data to model
use with caution
//this will drop the extra data field and all associated data with it $model->dropExtraField($name);
update extra field value
sometimes you may need to update the value of the extra field, you can achive that by using this :
#$extra_field @param String the name of the extra field #$updated_value @param String the new value $model->updateExtraValue($extra_field , $updated_value);
Custom models and migrations
You can change the models used by specifying a class name in the extra_model
& extra_value_model
key of
the extra-field
config file.
You can change the column name used in the extra_values table (model_id
by default) when using a custom migration
where you
changed that. In that case, simply change the model_primary_key_attribute
key of the extra-field
config file.
You can change the column name used in the extra_values table (model_class
by default) when using a custom migration
where you
changed that. In that case, simply change the model_name_attribute
key of the extra-field
config file.
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
You are welcome to contribute
Credits
License
The MIT License (MIT). Please see License File for more information.