hareku / laravel-bulk
The Laravel package for bulk insert/update.
v0.3.4
2024-07-15 04:54 UTC
Requires
- php: ^7.3|^8.0
- illuminate/contracts: ^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
- illuminate/database: ^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
- illuminate/support: ^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
Requires (Dev)
- mockery/mockery: ^1.4
- phpunit/phpunit: ^9.5
README
laravel-bulk
provides functions that execute bulk insert/update.
Packagist: https://packagist.org/packages/hareku/laravel-bulk
Installation
Composer: composer require hareku/laravel-bulk
.
Usage
EloquentBulk
EloquentBulk
provides bulk functions for Eloquent models.
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 // }, // ]
BulkProcessor
BulkProcessor
provides functions without Eloquent features (auto resolving table names, and filling timestamp columns).
So you have to give the table name.
use Hareku\Bulk\Facades\BulkProcessor; BulkProcessor::insert($tableName, $columns, $records); BulkProcessor::update($tableName, $indices, $records);
Contribution
Please open an issue or a pull request.
Local development
You can develop on VSCode remote container that already installed PHP and other dependencies. Otherwise, you have to install PHP and composer to the local, or run tests on Github Actions.
- Install dependencies:
composer install
- Test with PHPUnit:
composer test
- Test with PHPUnit and Xdebug coverage:
composer test:coverage-text
orcomposer test:coverage-html