PHP code example of hareku / laravel-bulk
1. Go to this page and download the library: Download hareku/laravel-bulk 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/ */
hareku / laravel-bulk example snippets
use Hareku\Bulk\Facades\EloquentBulk;
// \Illuminate\Database\Eloquent\Model
$model = new \App\Models\User;
$columns = ['name', 'age'];
$records = [
['john', 22],
['james', 23],
];
// EloquentBulk automatically resolves the table name,
// and fills `created_at` and `updated_at` columns.
EloquentBulk::insert($model, $columns, $records);
dump(User::all());
// [
// {
// id: 1,
// name: john,
// age: 22,
// created_at: 2020-01-01 12:00:00,
// updated_at: 2020-01-01 12:00:00
// },
// {
// id: 2,
// name: james,
// age: 23,
// created_at: 2020-01-01 12:00:00,
// updated_at: 2020-01-01 12:00:00
// },
// ]
$indices = ['id'];
// each record must have indices for sql's WHERE conditions.
$newRecords = [
[
'id' => 1,
'name' => 'new_john',
'age' => 25,
],
[
'id' => 2,
'name' => 'new_james',
],
];
EloquentBulk::update($model, $indices, $newRecords);
dump(User::all());
// [
// {
// id: 1,
// name: new_john,
// age: 25,
// created_at: 2020-01-01 12:00:00,
// updated_at: 2020-12-01 12:00:00
// },
// {
// id: 2,
// name: new_james,
// age: 23,
// created_at: 2020-01-01 12:00:00,
// updated_at: 2020-12-01 12:00:00
// },
// ]
use Hareku\Bulk\Facades\BulkProcessor;
BulkProcessor::insert($tableName, $columns, $records);
BulkProcessor::update($tableName, $indices, $records);