PHP code example of tmsllc / laravel-extra-field

1. Go to this page and download the library: Download tmsllc/laravel-extra-field library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tmsllc / laravel-extra-field example snippets


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',
];

use TMSLLC\ExtraField\HasExtraFields;

class YourEloquentModel extends Model
{
    use HasExtraFields;
}

$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');


$model->addStringExtraValue('fieldName' , 'filedValue');

//you can repeat for other object
$otherModel->addExtraValue('fieldName', 'filedValue');



// 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


//this wil drop the value of the given name extra field for this model
$model->dropExtraFieldData($name) ;


//this will drop the extra data field and all associated data with it
$model->dropExtraField($name);


#$extra_field @param String the name of the extra field
#$updated_value @param String the new value

$model->updateExtraValue($extra_field , $updated_value);
bash
php artisan vendor:publish --provider="TMSLLC\ExtraField\ExtraFieldServiceProvider" --tag="migrations"
bash
php artisan migrate
bash
php artisan vendor:publish --provider="TMSLLC\ExtraField\ExtraFieldServiceProvider" --tag="config"