PHP code example of mindtwo / laravel-dynamic-model-mutators

1. Go to this page and download the library: Download mindtwo/laravel-dynamic-model-mutators 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/ */

    

mindtwo / laravel-dynamic-model-mutators example snippets



namespace App;

use Illuminate\Database\Eloquent\Model;
use mindtwo\LaravelDynamicModelMutators\DynamicModelMutator;

class User extends Model
{
    use DynamicModelMutator;
}

/**
 * The "booting" method of the model.
 *
 * @return void
 */
protected static function boot()
{
    static::registerSetMutator('translations', 'setAttributeTranslation');
    static::registerGetMutator('translations', 'getAttributeTranslation');
}

namespace Examples;

use Illuminate\Database\Eloquent\Model;
use mindtwo\LaravelDynamicModelMutators\DynamicModelMutator;
use mindtwo\LaravelDynamicModelMutators\Interfaces\DynamicModelMutatorInterface; 

class exampleModel extends Model implements DynamicModelMutatorInterface 
{
    use use DynamicModelMutator;
    
    $translations = [
        'attribute_1',
        'attribute_2',
        'attribute_3'
    ];
}

namespace Examples;

use Illuminate\Database\Eloquent\Model;
use mindtwo\LaravelDynamicModelMutators\DynamicModelMutator;
use mindtwo\LaravelDynamicModelMutators\Interfaces\DynamicModelMutatorInterface; 

class exampleModel extends Model implements DynamicModelMutatorInterface 
{
    use use DynamicModelMutator;
    
    $translations = [
        'attribute_1' = 'string',
        'attribute_2' = 'text',
        'attribute_3' = [
            'type'    => string,
            'locales' => ['en', 'de'] 
        ]
    ];
}

/**
 * Set attribute translation.
 *
 * @return void
 */
public function setAttributeTranslation($name, $value, $config=null)
{
    // Set value $value for attribute $name 
}

/**
 * Get attribute translation.
 *
 * @return void
 */
public function setAttributeTranslation($name, $config=null)
{
    // Get value for attribute $name 
}