PHP code example of quince / data-importer

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

    

quince / data-importer example snippets


'providers' => array(
    'Quince\DataImporter\DataImporterServiceProvider'
)



// Initialize te/path/to/file/to/import';
$bindingModel = '/Namespace/ToYour/Model'; // or YourModel::class

// get data-importer out of IoC
$importer = App::make('importer');

/** @var RowsCollection $data */
$importer->import($filePath, $bindingModel, function($data) {
	// whatever you want to do with data
	// $data is an instance of RowsCollection
	// for example
	
	/** @var RowData $row */
	foreach ($data as $row) {
	    YourModel::create($row->getBase());
	}
});



use Quince\DataImporterManager as Importer;

class ExampleClass {

	protected $importer

	public function __construct(Importer $importer)
	{
		$this->importer = $importer;
	}

	public function exampleMethod($file)
	{
		$this->importer->import($file->getPath(), DesiredModel::class, function($data) {
			// whatever you want to do with data
		});
	}

}



// Initialize te/path/to/file/to/import';
$bindingModel = '/Namespace/ToYour/Model'; // or YourModel::class
$additionalFields = [
	'base' => [
		'column_name' => 'value'
	],
	'relation' => [
		'relation_name' => [
			'column_name' => 'value'
		]
		// another relations goes heare
	]
];

// get data-importer out of IoC
$importer = App::make('importer');

$importer->setAdditionalFields($additionalFields)
         ->import($filePath, $bindingModel, function($data) {
         	// whatever you want to do with data
         });

$importer->setAdditionalFields($additionalFields, true)
         ->import($filePath, $bindingModel, function($data) {
         	// whatever you want to do with data
         });



// Initialize te/path/to/file/to/import';
$bindingModel = '/Namespace/ToYour/Model'; // or YourModel::class
$dictionary = [
	'Real Name'		=> 'name',
	'Nickname'		=> 'username',
	'Mail Address'	=> 'email',
	'Phone Numbers'	=> 'phones.number'
];

// get data-importer out of IoC
$importer = App::make('importer');

$importer->setDictionary($dictionary)
         ->import($filePath, $bindingModel, function($data) {
         	// whatever you want to do with data
         });

$importer->setColumnDictionary($columnName, $translation)
         ->import($filePath, $bindingModel, function($data) {
         	// whatever you want to do with data
         });
bash
$ php artisan config:publish quince/data-importer
bash
$ php artisan vendor:publish