PHP code example of taylornetwork / backup-importer

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

    

taylornetwork / backup-importer example snippets


use TaylorNetwork\BackupImporter\BaseImporter;

class CustomerImporter extends BaseImporter
{
    public function import(): int
    {
        return $this->simpleImport();
    }
}

protected $model = App\Models\Customer::class;

protected $backupTableName = 'xyz_customers';

protected $ignoreModel = true;

public function getColumnMap(): array
{
    return [
        'firstname as first_name,
        'lastname as last_name',
        'address',
    ];
}

use TaylorNetwork\BackupImporter\BaseImporter;

class CustomerImporter extends BaseImporter
{
    /**
     * Set the model to import to
     */
    protected $model = App\Models\Customer::class;
    
    /**
     * Set the backup table name
     */
    protected $backupTableName = 'xyz_customers';
    
    /**
     * Set the columns to get from the backup table
     */
    public function getColumnMap(): array
    {
        return [
            'firstname as first_name',
            'lastname as last_name',
            'address',
        ];
    }

    
    public function import(): int
    {
       return $this->simpleImport();    
    }
}

// App\Backup\Importers\CustomerServiceImporter.php

use TaylorNetwork\BackupImporter\BaseImporter;
use App\Customer;

class CustomerServiceImporter extends BaseImporter
{
    public function getColumnMap(): array
    {
        return [
            'customer_id',
            'service_id',
            'qty',
            'description',
            'last_service_date',
        ];
    }
    
    public function import(): int
    {
        $rows = $this->select()->where('last_service_date', '!=', null)->get();
        
        foreach($rows as $row) {
            Customer::find($row->customer_id)->services()->create([
                'service_id' => $row->service_id,
                'qty' => $row->qty,
                'desc' => $row->description,
                'last_date' => $row->last_service_date,
            ]);
            
            $this->increment();
        }
        
        return $this->getImportTotal();
    }
}
bash
$ php artisan vendor:publish
bash
$ php artisan importer:new CustomerImporter