hareku / laravel-bulk
用于批量插入/更新的Laravel包。
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
提供执行批量插入/更新的函数。
Packagist: https://packagist.org.cn/packages/hareku/laravel-bulk
安装
Composer: composer require hareku/laravel-bulk
。
使用方法
EloquentBulk
EloquentBulk
提供Eloquent模型的批量函数。
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
提供不带Eloquent功能的函数(自动解析表名和填充时间戳列)。因此,您必须提供表名。
use Hareku\Bulk\Facades\BulkProcessor; BulkProcessor::insert($tableName, $columns, $records); BulkProcessor::update($tableName, $indices, $records);
贡献
请打开一个问题或拉取请求。
本地开发
您可以在已安装PHP和其他依赖项的VSCode远程容器上进行开发。否则,您必须在本地安装PHP和composer,或在Github Actions上运行测试。
- 安装依赖项:
composer install
- 使用PHPUnit进行测试:
composer test
- 使用PHPUnit和Xdebug覆盖率测试:
composer test:coverage-text
或composer test:coverage-html