PHP code example of hxm / extrafield

1. Go to this page and download the library: Download hxm/extrafield 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/ */

    

hxm / extrafield example snippets



function boot()
{
    \HXM\ExtraField\ExtraField::$ignoreMigration = true;
}


use HXM\ExtraField\Contracts\CanMakeExtraFieldInterface;
use HXM\ExtraField\Traits\HasExtraField;

class AnyModel extends Model implements CanMakeExtraFieldInterface
{
    use HasExtraField;
}

use HXM\ExtraField\Contracts\CanMakeExtraFieldByInstanceInterface;
use HXM\ExtraField\Traits\HasExtraFieldByInstance;

class AnyModel extends Model implements CanMakeExtraFieldByInstanceInterface
{
    use HasExtraFieldByInstance;
}

use HXM\ExtraField\Contracts\CanMakeExtraFieldInterface;
use HXM\ExtraField\Traits\HasExtraField;

class ParentModel extends Model implements CanMakeExtraFieldInterface
{   
    use HasExtraField;
    
    public function type()
    {
        return $this->belongsTo(AnyModel::class);
    }
    
    public function getExtraFieldTargetTypeInstance(): CanMakeExtraFieldInterface
    {
        return $this->type;
    }
}

...
use HXM\ExtraField\Actions\UpdateOrCreateFieldAction;
use Illuminate\Http\Request;
use AnyModel;

class Controller ... 
{
    function createOrUpdateExtraField(AnyModel $modelHasExtraField, Request $request, UpdateOrCreateFieldAction $action)
    {
        $action->handle($modelHasExtraField, $request, $allowMissingFields);
    }
}

use HXM\ExtraField\Contracts\CanMakeExtraFieldByInstanceInterface;
use HXM\ExtraField\Traits\HasExtraFieldByInstance;
use HXM\ExtraField\Contracts\CanAccessExtraFieldValueInterface;
use HXM\ExtraField\Traits\HasExtraFieldValue;
use HXM\ExtraField\Traits\AutoValidationAndSaveExtraFieldValue;

class AnyModel extends Model implements CanMakeExtraFieldByInstanceInterface, CanAccessExtraFieldValueInterface
{
    use HasExtraFieldByInstance;
    use HasExtraFieldValue;
    // use AutoValidationAndSaveExtraFieldValue; 
    // Automatically save Extra Values when model saved
}


use HXM\ExtraField\Contracts\CanMakeExtraFieldInterface;
use HXM\ExtraField\Contracts\CanAccessExtraFieldValueInterface;
use HXM\ExtraField\Traits\HasExtraFieldValue;
use HXM\ExtraField\Traits\AutoValidationAndSaveExtraFieldValue;

class ParentModel extends Model implements CanAccessExtraFieldValueInterface
{
    use HasExtraFieldValue;
    // use AutoValidationAndSaveExtraFieldValue; 
    // Automatically save Extra Values when model saved
    
    public function type()
    {
        return $this->belongsTo(AnyModel::class);
    }
    
    public function getExtraFieldTargetTypeInstance(): CanMakeExtraFieldInterface
    {
        return $this->type;
    }
}

...
use HXM\ExtraField\ExtraFieldValueValidation;
use HXM\ExtraField\Actions\SaveExtraFieldValueForTargetAction;
use Illuminate\Http\Request;
use AnyModel;

class Controller ... 
{
    function store(Request $request, SaveExtraFieldValueForTargetAction $action)
    {
        $model = AnyModel::create([
                ...
            ]);
        $validation = new ExtraFieldValueValidation($model, $request->all());
        $action->handle($model, $validation);
    }
}

//extra_field.php
...
return [
    ...
    
    'enums' => [
        AnyModel::class => new \App\Enums\DemoExtraFieldTypeEnums(),
    ]
]


/**
 * Created by HoanXuanMai
 * @author [email protected]
 */
namespace App\Enums;

use HXM\Enum\Abstracts\EnumBase;
use HXM\ExtraField\Contracts\ExtraFieldTypeEnumInterface;
use HXM\ExtraField\Models\ExtraField;


class DemoExtraFieldTypeEnums extends EnumBase implements ExtraFieldTypeEnumInterface
{
    const SECTION = 'SECTION';
    const TEXT = 'TEXT';
    const NUMBER = 'NUMBER';
    const SELECT = 'SELECT';
    const MULTIPLE = 'MULTIPLE';
    ...

    protected static $descriptions = [
        'SECTION' => 'Section',
        'TEXT' => 'Text',
        'NUMBER' => 'Number',
        'SELECT' => 'Select',
        'MULTIPLE' => 'Multiple choice',
        ...
    ];

    static function buildSelectOptions(): \Illuminate\Support\Collection
    {
        return static::getCollection(function($des, $value) {
            return [
                'component' => self::getComponentByType($value),
                'ue) : bool
    {
        return $value == self::FILE;
    }

    static function inputRequestIsMultiple($value) : bool
    {
        return ...
    }

    static function appendToArray(ExtraField $extraField): array
    {
        return [
            'component' => static::getComponentByType($extraField->type),
        ];
    }
}


//extra_field.php
...
return [
    ...
    
    'valueProcessions' => [
        \App\Models\Demo::class => new \App\Casts\DemoExtraValueProcessValue(),
    ]
]


/**
 * Created by HoanXuanMai
 * @author [email protected]
 */

namespace App\Casts;

use HXM\ExtraField\Contracts\ExtraValueProcessValueInterface;
use HXM\ExtraField\Models\ExtraField;
use HXM\ExtraField\Models\ExtraFieldValue;

class DemoExtraValueProcessValue implements ExtraValueProcessValueInterface
{

    function getValue($value, $valueType = null, ExtraFieldValue $model)
    {
        return $value;
    }

    function setValue($value, $valueType = null, ExtraField $model)
    {
     
        return $value;
    }
}


$ php artisan migrate

$ php artisan vendor:publish --tag=extra_field:config