PHP code example of crazybooot / laravel-mapped-model-fields

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

    

crazybooot / laravel-mapped-model-fields example snippets



declare(strict_types = 1);

namespace App\Models;

use Crazybooot\MappedModelFields\Contracts\HasMappedFields;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

/**
 * Class Product
 *
 * @package App\Models
 */
class Product extends Model implements HasMappedFields
{
        /**
         * @return array
         */
        public function getFieldsMap(): array
        {
            return [
                'vendor_id'         => 'identifier',
                'title'             => 'information.title',
                'vendor_created_at' => 'created_at',
            ];
        }
}

$externalProduct = [
    'identifier'        => 443765834,
    'information'     => [
        'title' => 'Some awesome product',
    ],
    'crated_at' => '2018-12-21 14:36:04',
];
//model method to map values from source and create entitie
$product = Product::mapAndCreate($externalProduct);

//relation method to map values from source and create related entitie
$product = $user->products()->mapAndCreate($externalProduct);

//model method to map values from source and update or create entitie
$product = Product::mapAndUpdateOrCreate(
    ['vendor_id' => $externalProduct['identifier']], 
    $externalProduct
);

//relation method to map values from source and update or create related entitie
$product = $user
    ->products()
    ->mapAndUpdateOrCreate(
        ['vendor_id' => $externalProduct['identifier']], 
        $externalProduct
    );
    
//model method to map values and update enitie
$product->mapAndUpdate($externalProduct);

$externalProduct = [
    'identifier'        => 443765834,
    'information'     => [
        'title' => 'Some awesome product',
    ],
    'crated_at' => '2018-12-21 14:36:04',
];
//append value to resulting array after mapping
$product = Product::mapAndCreate($externalProduct, ['user_id' => $user->getKey()]);
    
//axclude specified source value from mapping
$product->mapAndUpdate($externalProduct, [], ['information.title']);

        /**
         * @return array
         */
        public function getFieldsMap(): array
        {
            return [
                'vendor_id'         => 'identifier',
                'title'             => [
                    'key'       => 'informaion.title',
                    'transform' => function($value) {
                        return str_upper($value);
                    }
                ],
                'vendor_created_at' => 'created_at',
            ];
        }