PHPackages                             free3\_5man/laravel-eloquent-batch - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. free3\_5man/laravel-eloquent-batch

ActiveLibrary

free3\_5man/laravel-eloquent-batch
==================================

a common-used batch util is provided for batch insert/update in multiple databases, also with laravel eloquent based batch action

v1.0.2(4y ago)238MITPHPPHP &gt;=7.1

Since Oct 8Pushed 1y ago1 watchersCompare

[ Source](https://github.com/free3-5man/laravel-eloquent-batch)[ Packagist](https://packagist.org/packages/free3_5man/laravel-eloquent-batch)[ RSS](/packages/free3-5man-laravel-eloquent-batch/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (3)Versions (4)Used By (0)

Laravel Eloquent BATCH
======================

[](#laravel-eloquent-batch)

[![Build Status](https://camo.githubusercontent.com/3c57915ecb5ff779d04f8f8d2b97f0d175f575649b6ea003c1910edb70d6af68/68747470733a2f2f6170702e7472617669732d63692e636f6d2f66726565332d356d616e2f6c61726176656c2d656c6f7175656e742d62617463682e7376673f6272616e63683d6d6173746572)](https://app.travis-ci.com/free3-5man/laravel-eloquent-batch)

provide an util for batch Insert/update, also an eloquent collection macro batchSave for batch action more convenient in Laravel.

Install
=======

[](#install)

`composer require free3_5man/laravel-eloquent-batch`

Service Provider
================

[](#service-provider)

file app.php in array providers :

`Freeman\LaravelBatch\BatchServiceProvider::class,`

Features
========

[](#features)

- make the insert data items assoc with keys, which has the same data structure with update data
- return insert ids when primary key is integer auto\_increment
- MySQL, PostgreSQL, SQLite and SQL Server is supported
- json/jsonb field is supported
- auto chunked when generate batch sql
- support Eloqunent Collection macro batchSave for auto batchInsert and batchUpdate
- batchSave fill items with insert id when actually insert and primary key is integer auto\_increment
- batchSave return updated models when actually update
- batchSave can save inserts and updates at the same time
- batchSave can control whether to touch updated\_at with the param $touchUpdatedAt

Batch Insert Example
====================

[](#batch-insert-example)

```
use App\Models\Player;
use Freeman\LaravelBatch\BatchUtil;

$data = [
    // this is the first item, columns of this item will be used as insert columns
    // if columns of any other items does not equal to this, false will be returned as result
    // all the columns of each item required to be same
    [
        'name' => 'kaka',
        'birthday' => '1982-04-22',
    ],
    [
        'name' => 'nesta',
        'birthday' => '1976-03-19',
    ],
    [
        'name' => 'pirlo',
        'birthday' => '1979-05-19',
    ],
];
// batchInsert use the first item columns as insert columns default;
$ids = BatchUtil::ofModel(new Player)->batchInsert($data);
// if players primay key auto_increment, insert ids array ([1, 2, 3]) will be returned, else empty array ([]) will be returned

$data[1]['is_captain'] = true;
BatchUtil::ofModel(new Player)->batchInsert($data); // return false
```

Batch Update Example
====================

[](#batch-update-example)

```
$data = [
    [
        'id' => 1,
        'name' => 'kaka',
        'birthday' => '1982-04-22',
    ],
    [
        'id' => 2,
        'name' => 'nesta',
        'birthday' => '1976-03-19',
    ],
    [
        'id' => 3,
        'name' => 'pirlo',
        'birthday' => '1979-05-19',
    ],
];
BatchUtil::ofModel(new Player)->batchUpdate($data);
```

batchSave Example
=================

[](#batchsave-example)

```
$data = [
    [
        'name' => 'kaka',
        'birthday' => '1982-04-22',
    ],
    [
        'name' => 'nesta',
        'birthday' => '1976-03-19',
    ],
    [
        'name' => 'pirlo',
        'birthday' => '1979-05-19',
    ],
];
// batchSave will do batch insert here
$players = collect($data)->map(function($item) {
    return new Player($item);
})->batchSave();    // each player has id
dump($players->pluck('id')->toArray()); // [1, 2, 3]

// batchSave will do batch update here, updated_at will be touched automaticly
$players->map(function($player) {
    return $player->fill([
        'name' => $player->name . '-' . $player->id,
    ]);
})->batchSave();
dump($players->pluck('name')->toArray());   // ['kaka-1', 'nesta-2', 'pirlo-3']
```

Testing
=======

[](#testing)

```
$ composer test
```

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance29

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~76 days

Total

3

Last Release

1526d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8b46b8e991f7097aa77e8fb50bb1436c6d97a25105be600b7fc67016464b447d?d=identicon)[free3\_5man](/maintainers/free3_5man)

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/free3-5man-laravel-eloquent-batch/health.svg)

```
[![Health](https://phpackages.com/badges/free3-5man-laravel-eloquent-batch/health.svg)](https://phpackages.com/packages/free3-5man-laravel-eloquent-batch)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
