PHP code example of oguzhankrcb / datamigrator

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

    

oguzhankrcb / datamigrator example snippets


$toModelPrototype = [
    'id'         => '[id]',
    'unique_id'  => '[unique_number.id]',
    'name'       => '[data->name]',
    'categories' => [
        'first_category'  => '[data->categories->category_2]',
        'second_category' => '[data->categories->category_3]',
    ],
    'alias_with_item_code' => '[data->alias][data->item->code]',
    'alias'                => '[data->alias]',
    'item_code'            => '[data->item->code]',
    'status'               => '[data->status]',
];

$fromModel = [
    'id'            => 1,
    'unique_number' => 'lxAxmUlkfc',
    'data'          => [
        'name'       => 'John Doe',
        'alias'      => 'JD',
        'categories' => [
            'category_1' => 'Bronze',
            'category_2' => 'Silver',
            'category_3' => 'Gold',
        ],
        'item' => [
            'code' => 196854,
        ],
        'status' => true,
    ],
];

use Oguzhankrcb\DataMigrator\Facades\DataMigrator;

$newData = DataMigrator::transformData($toModelPrototype, $fromModel);

[
    'id'         => 1,
    'unique_id'  => 'lxAxmUlkfc1',
    'name'       => 'John Doe',
    'categories' => [
        'first_category'  => 'Silver',
        'second_category' => 'Gold',
    ],
    'alias_with_item_code' => 'JD196854',
    'alias'                => 'JD',
    'item_code'            => '196854',
    'status'               => true,
]

$transferToModel = \App\Models\User::class;

$transferFromModel = \App\Models\LegacyUser::class;

use App\Models\Order;
use App\Models\Invoice;
use Oguzhankrcb\DataMigrator\Facades\DataMigrator;

// Define the fields to transfer from Order to Invoice
$toModelPrototype = [
    'invoice_number' => '[order_number]',
    'customer_name' => '[customer->name]',
    'customer_email' => '[customer->email]',
    'total_amount' => '[amount]',
    'total_amount_with_currency' => '[amount]€',
];

// Transfer the data from Order to Invoice
DataMigrator::transferAllDataFromModelToModel(Invoice::class, $toModelPrototype, Order::class);

use App\Models\Order;
use App\Models\Invoice;
use Oguzhankrcb\DataMigrator\Facades\DataMigrator;

// Define the fields to transfer from Order to Invoice
$toModelPrototype = [
    'invoice_number' => '[order_number]',
    'customer_name' => '[customer->name]',
    'customer_email' => '[customer->email]',
    'total_amount' => '[amount]',
    'total_amount_with_currency' => '[amount]€',
];

$orderInstance = Order::find(1);

// Transfer the data from Order to Invoice
$transferedModel = DataMigrator::transferDataModelToModel(Invoice::class, $toModelPrototype, $orderInstance);