PHPackages                             wfeller/laravel-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. [Queues &amp; Workers](/categories/queues)
4. /
5. wfeller/laravel-batch

ActiveLibrary[Queues &amp; Workers](/categories/queues)

wfeller/laravel-batch
=====================

Insert, update or delete models in batch, while still firing model events.

7.1.0(1w ago)54.3k1MITPHPPHP ^8.3CI failing

Since Nov 18Pushed 1w ago2 watchersCompare

[ Source](https://github.com/wfeller/laravel-batch)[ Packagist](https://packagist.org/packages/wfeller/laravel-batch)[ Docs](https://github.com/wfeller/laravel-batch)[ RSS](/packages/wfeller-laravel-batch/feed)WikiDiscussions master Synced today

READMEChangelog (10)Dependencies (14)Versions (38)Used By (0)

Laravel Batch
=============

[](#laravel-batch)

[![Latest Version on Packagist](https://camo.githubusercontent.com/9c1b52b20c6d2d4ed7e95c3dfde0b4de2211de3b8fbe831ecce9f77dcb781017/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7766656c6c65722f6c61726176656c2d62617463682e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/wfeller/laravel-batch)[![Total Downloads](https://camo.githubusercontent.com/c4882a1aa97f22724063e4606e7106784626c976d6598af7f98158e7ef95a3e9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7766656c6c65722f6c61726176656c2d62617463682e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/wfeller/laravel-batch)[![Plant Tree](https://camo.githubusercontent.com/82b7e7c30d073f74b47bbaaf70f5f98792426ff1bea8ee7b05c5879ac56a060c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f64796e616d69632f6a736f6e3f636f6c6f723d627269676874677265656e266c6162656c3d506c616e74253230547265652671756572793d2532342e746f74616c2675726c3d68747470732533412532462532467075626c69632e6f66667365742e6561727468253246757365727325324674726565776172652532467472656573)](https://plant.treeware.earth/wfeller/laravel-batch)[![Buy us a tree](https://camo.githubusercontent.com/15453546808b5ea47b48633f72f490420e2e41b885556eee95d7e88f4a754418/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f54726565776172652d2546302539462538432542332d6c69676874677265656e3f7374796c653d666f722d7468652d6261646765)](https://plant.treeware.earth/wfeller/laravel-batch)

Save and update your Eloquent models in batches while still firing model events.

Introduction
------------

[](#introduction)

This package allows you to efficiently save, update, and delete many Eloquent models at once while maintaining Laravel's standard model event system. Unlike Laravel's native `insert()` method, this package fires all the normal model events (`saving`, `saved`, `creating`, `created`, `updating`, `updated`, `deleting`, `deleted`).

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require wfeller/laravel-batch
```

Quick Start
-----------

[](#quick-start)

```
use App\Models\User;
use WF\Batch\Batch;

// Save multiple models at once
$users = [
    ['name' => 'John', 'email' => 'john@example.com'],
    ['name' => 'Jane', 'email' => 'jane@example.com'],
    $existingUser // existing model instance
];

$userIds = Batch::of(User::class, $users)->save()->now();
```

Core Features
-------------

[](#core-features)

### 1. Batch Saving Models

[](#1-batch-saving-models)

Save multiple models with a single operation:

```
use App\Models\Car;
use WF\Batch\Batch;

$cars = [
    ['brand' => 'Audi', 'model' => 'A6'],
    ['brand' => 'Ford', 'model' => 'Mustang'],
    $existingCar // existing model instance
];

// Save immediately
$carIds = Batch::of(Car::class, $cars)->save()->now();

// Set custom batch size
$carIds = Batch::of(Car::class, $cars)->batchSize(100)->save()->now();
```

### 2. Batch Updating Models

[](#2-batch-updating-models)

Update multiple existing models:

```
use App\Models\User;
use WF\Batch\Batch;

$users = [
    ['id' => 1, 'name' => 'Updated John'],
    ['id' => 2, 'name' => 'Updated Jane'],
    $userInstance // existing model with changes
];

$updatedIds = Batch::of(User::class, $users)->save()->now();
```

### 3. Batch Deleting Models

[](#3-batch-deleting-models)

Delete multiple models efficiently:

```
use App\Models\Car;
use WF\Batch\Batch;

// Delete by IDs
$carIds = [1, 2, 3, 5, 8];
$deletedIds = Batch::of(Car::class, $carIds)->delete()->now();

// Delete by model instances
$cars = Car::find([1, 2, 3]);
$deletedIds = Batch::of(Car::class, $cars)->delete()->now();

// Mixed approach
$mixed = [1, $carInstance, 3, $anotherCar];
$deletedIds = Batch::of(Car::class, $mixed)->delete()->now();
```

### 4. Queue Support

[](#4-queue-support)

Process batch operations in the background:

```
use App\Models\User;
use WF\Batch\Batch;

$users = [
    ['name' => 'John', 'email' => 'john@example.com'],
    ['name' => 'Jane', 'email' => 'jane@example.com']
];

// Dispatch to default queue
Batch::of(User::class, $users)->save()->dispatch();

// Dispatch to specific queue
Batch::of(User::class, $users)->save()->onQueue('high-priority')->dispatch();
```

### 5. Model Trait Integration

[](#5-model-trait-integration)

Add batch functionality directly to your models:

```
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use WF\Batch\Traits\Batchable;

class Car extends Model
{
    use Batchable;

    // ...
}

// Now you can use batch operations directly on the model
$cars = [
    ['brand' => 'Audi', 'model' => 'A6'],
    ['brand' => 'Ford', 'model' => 'Mustang']
];

// These are equivalent
$carIds = Car::newBatch($cars)->save()->now();
$carIds = Car::batchSave($cars);

// For deletion
Car::batchDelete([1, 2, 3]);
```

### 6. Batch Size Configuration

[](#6-batch-size-configuration)

Control how many models are processed in each batch:

```
use App\Models\User;
use WF\Batch\Batch;

$users = collect()->range(1, 10000)->map(fn($i) => [
    'name' => "User $i",
    'email' => "user$i@example.com"
]);

// Process in batches of 500 (default)
Batch::of(User::class, $users)->save()->now();

// Process in batches of 1000
Batch::of(User::class, $users)->batchSize(1000)->save()->now();

// Set global default batch size
Batch::setDefaultBatchSize(1000);
```

Performance Comparison
----------------------

[](#performance-comparison)

MethodSpeedModel EventsLaravel's `insert()`FastestNoLaravel Batch1.3-3x slower than `insert()`YesIndividual `create()` calls8-50x slower than `insert()`Yes```
// Fastest but no events
User::insert([$userA, $userB, $userC]);

// Balanced: good performance with events
Batch::of(User::class, [$userA, $userB, $userC])->save()->now();

// Slowest: individual operations
foreach ([$userA, $userB, $userC] as $user) {
    User::create($user);
}
```

Event Handling
--------------

[](#event-handling)

All standard Laravel model events are fired during batch operations:

```
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected static function booted()
    {
        static::creating(function (User $user) {
            // Called for each new user in batch
            $user->created_by = auth()->id();
        });

        static::updating(function (User $user) {
            // Called for each updated user in batch
            $user->updated_by = auth()->id();
        });

        static::deleting(function (User $user) {
            // Called for each user being deleted in batch
            $user->deleted_by = auth()->id();
        });
    }
}
```

Testing
-------

[](#testing)

Run the test suite:

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email me instead of using the issue tracker.

License
-------

[](#license)

This package is [Treeware](https://treeware.earth). If you use it in production, then we ask that you [**buy the world a tree**](https://plant.treeware.earth/wfeller/laravel-batch) to thank us for our work. By contributing to the Treeware forest you'll be creating employment for local families and restoring wildlife habitats.

You can buy trees here [offset.earth/treeware](https://plant.treeware.earth/%7Bvendor%7D/%7Bpackage%7D)

Read more about Treeware at [treeware.earth](http://treeware.earth)

Credits
-------

[](#credits)

- [William](https://github.com/wfeller)
- [All Contributors](../../contributors)

###  Health Score

60

—

FairBetter than 98% of packages

Maintenance98

Actively maintained with recent releases

Popularity26

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity88

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

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 ~75 days

Total

37

Last Release

13d ago

Major Versions

2.0.2 → 3.02019-09-03

3.0 → 4.02019-09-05

4.2.1 → 5.0.02020-03-22

5.0.0 → 6.02020-04-10

6.4.4 → 7.02022-02-16

PHP version history (5 changes)1.0.0PHP ^7.2

5.0.0PHP ^7.4

6.4.4PHP ^7.4|^8.0

7.0PHP ^8.0

7.1.0PHP ^8.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/61f8f46bedb67ee0a5472bef1ba54cf68f8594514c57404661606721dec7c7d8?d=identicon)[wfeller](/maintainers/wfeller)

---

Top Contributors

[![wfeller](https://avatars.githubusercontent.com/u/21337627?v=4)](https://github.com/wfeller "wfeller (41 commits)")

---

Tags

laravelqueuebatch

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/wfeller-laravel-batch/health.svg)

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

###  Alternatives

[laravel/scout

Laravel Scout provides a driver based solution to searching your Eloquent models.

1.7k49.4M479](/packages/laravel-scout)[imtigger/laravel-job-status

Laravel Job Status

5272.1M2](/packages/imtigger-laravel-job-status)[yadahan/laravel-authentication-log

Laravel Authentication Log provides authentication logger and notification for Laravel.

416632.8k5](/packages/yadahan-laravel-authentication-log)[harris21/laravel-fuse

Circuit breaker for Laravel queue jobs. Protect your workers from cascading failures.

3786.5k](/packages/harris21-laravel-fuse)[shaffe/laravel-mail-log-channel

A package to support logging via email in Laravel

1286.2k](/packages/shaffe-laravel-mail-log-channel)[dnxlabs/laravel-queue-aws-batch

Laravel Queue for AWS Batch, enabling users to submit jobs for processing to a Batch queue.

145.8k](/packages/dnxlabs-laravel-queue-aws-batch)

PHPackages © 2026

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