PHP code example of yajra / laravel-sql-loader

1. Go to this page and download the library: Download yajra/laravel-sql-loader 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/ */

    

yajra / laravel-sql-loader example snippets


Route::get('sql-loader', function () {
    Schema::dropIfExists('employees');
    Schema::create('employees', function ($table) {
        $table->id();
        $table->string('name');
        $table->integer('dept_id');
        $table->timestamps();
    });

    Yajra\SQLLoader\CsvFile::make(database_path('files/employees.csv'), 'w')
        ->headers(['name', 'dept_id', 'created_at', 'updated_at'])
        ->insert([
            ['John Doe', 1, now(), now()],
            ['Jane Doe', 2, now(), now()],
            ['John Doe', 1, now(), now()],
            ['Jane Doe', 2, now(), now()],
        ])
        ->close();

    $loader = Yajra\SQLLoader\SQLLoader::make();
    $loader->inFile(database_path('files/employees.csv'))
        ->dateFormat('YYYY-MM-DD HH24:MI:SS')
        ->withHeaders()
        ->into('employees')
        ->execute();

    return DB::table('employees')->get();
});

$loader->dateFormat('YYYY-MM-DD HH24:MI:SS');

$loader->options(['skip=1', 'load=1000']);

$loader->inFile(database_path('files/employees.csv'));

$loader->inFile(database_path('files/employees.csv'))
    ->inFile(database_path('files/departments.csv')),

$loader->mode(Yajra\SQLLoader\Mode::TRUNCATE);

$loader->into('employees', ['name', 'dept_id']);

$users = User::all();
Yajra\SQLLoader\CsvFile::make(database_path('files/users.csv'), 'w')
    ->headers(array_keys($users->first()->toArray()))
    ->insert($users->toArray())
    ->close();

$loader->inFile(database_path('files/users.csv'))
    ->withHeaders()
    ->mode(Yajra\SQLLoader\Mode::TRUNCATE)
    ->connection('backup')
    ->into('users')
    ->execute();

$loader->inFile(database_path('files/*.csv'))
    ->withHeaders()
    ->mode(Yajra\SQLLoader\Mode::TRUNCATE)
    ->into('employees')
    ->execute();

$loader->withHeaders()
    ->constants([
        'file_id CONSTANT 1',
        'created_at EXPRESSION "current_timestamp(3)"',
        'updated_at EXPRESSION "current_timestamp(3)"',
    ])
    ->into('users');

$loader->connection('oracle');

$loader->disk('local');

return nl2br($loader->logs());

$loader->as('employees.ctl');

$loader->execute();

$loader->execute(60);

if ($loader->successfull()) {
    return 'Data loaded successfully!';
}

$result = $loader->result();

$loader = Yajra\SQLLoader\SQLLoader::make();
$loader->beginData([
        ['John', 1],
        ['Jane', 1],
        ['Jim, K', 2],
        ['Joe', 2],
    ])
    ->mode(Yajra\SQLLoader\Mode::TRUNCATE)
    ->into('employees', [
        'name',
        'dept_id',
    ])
    ->execute();

'connection' => env('SQL_LOADER_CONNECTION', 'oracle'),

'sqlldr' => env('SQL_LOADER_PATH', '/usr/local/bin/sqlldr'),

'disk' => env('SQL_LOADER_DISK', 'local'),
bash
php artisan vendor:publish --provider="Yajra\SQLLoader\SQLLoaderServiceProvider" --tag="config"