n1215/eloquent-bulk-save

使用 Eloquent 模型批量插入多个记录。

v0.1.0 2018-07-02 11:48 UTC

This package is auto-updated.

Last update: 2024-09-19 13:24:11 UTC


README

Latest Stable Version

此库包含一个特质,它可以使 \Illuminate\Database\Eloquent\Model 在一个查询中批量插入多个记录。与直接使用 \Illuminate\Database\Query\Builder::insert() 的情况不同,此特质方法会触发每个 Eloquent 模型事件,如 eloquent.creating、eloquent.saving 等。

用法

// 1. change your eloquent model class to use the trait
class YouModel extends \Illuminate\Database\Eloquent\Model
{
    use \N1215\EloquentBulkSave\BulkInsert;
}

// 2. create model collection (you can also use Illuminate\Database\Eloquent\Collection)
$models = \Illuminate\Support\Collection::make([
    new YourModel($attributes1),
    new YourModel($attributes2),
    ...
]);

// 3. use bulkInsert() static method
YourModel::bulkInsert($models);

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 LICENSE