Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Laravel Batch

Laravel Batch Laravel Package

mavinoo/laravel-batch

Laravel Batch provides bulk insert and update helpers for Laravel models. Use Batch::update with a key column or updateMultipleCondition for per-row conditions, making large data changes faster and cleaner without repetitive queries. Includes a facade and batch() helper.

View on GitHub
Deep Wiki
Context7

mavinoo/laravel-batch is a lightweight Laravel package for performing bulk (batch) inserts and updates through a clean, expressive API. Instead of looping over records and issuing one query per row, you pass an array of data and the package builds a single SQL statement—ideal for syncing large datasets quickly. It works with Eloquent models and supports both facade and batch() helper syntax.

  • Batch update records matched by an index column
  • updateMultipleCondition for per-row conditions/columns
  • Works with any Eloquent model instance
  • Supports Carbon dates and mixed column sets
  • Drop-in service provider and alias setup
Frequently asked questions about Laravel Batch
How do I install mavinoo/laravel-batch in a Laravel app?
Install it with Composer by running composer require mavinoo/laravel-batch. Then add Mavinoo\Batch\BatchServiceProvider::class to the providers array in config/app.php and, if you want the facade, register 'Batch' => Mavinoo\Batch\BatchFacade::class in the aliases array. After that you can call Batch::update() or use the batch() helper anywhere in your application.
What is the difference between Batch::update and updateMultipleCondition?
Batch::update takes an array of rows that all share the same column set and applies them against a single index column such as id. updateMultipleCondition lets every entry define its own conditions array and columns array, so each row can be matched on different criteria and update different fields. Use update for uniform bulk edits and updateMultipleCondition when individual rows need their own WHERE clause.
My bulk update is very slow on PostgreSQL or with large batches. What changed in v3.1.0?
Each UPDATE ... CASE statement now holds at most 100 rows, which fixes the super-linear slowdown large statements suffered when the database evaluated rows against many CASE branches. In the published 50,000-row benchmark, PostgreSQL 16 went from over 15 minutes to 2.5 seconds, MySQL 8.4 from 30.5s to 3.1s, and MariaDB 10.11 from 25.3s to 2.4s. Laravel Batch also splits batches automatically when they would exceed the database bound-parameter limit, inside a single transaction on the model's connection, and you can tune the batch size (default 500, minimum 100).
Does laravel-batch support upsert, i.e. insert or update if the row already exists?
Yes. upsert() inserts new rows and updates existing ones in a single query, so idempotent sync and import jobs no longer need a read-then-write dance or a per-row existence check. It keeps correct timestamp semantics by setting created_at only on new rows, supports enums, and splits large batches automatically inside one transaction.
How can I preview the SQL a batch operation will run before executing it?
Call pretend() to get back the SQL statement and bindings a call would produce, without executing it or even connecting to the database. That makes it practical to review generated SQL during code review, assert on it in CI, and validate a destructive batch before it touches production.
Can I batch update or delete rows by composite keys instead of a single primary key?
Yes. updateByKeys() and deleteByKeys() match on any number of key columns rather than a single primary key. That brings tenant-scoped tables, pivot tables and natural or composite-key tables into scope, removing a whole category of cases that previously could not be batched with this package.
Does laravel-batch respect SoftDeletes when deleting rows?
deleteByKeys() respects the SoftDeletes trait, so models using it are soft deleted unless you pass $force as true. Be aware that this coverage applies to delete operations only, because the update paths do not filter on deleted_at.
Which databases has laravel-batch been tested against?
Version 3.x runs an explicit CI matrix across MySQL, MariaDB, PostgreSQL and SQLite, with benchmark figures published for MySQL 8.4, MariaDB 10.11, PostgreSQL 16 and SQLite 3.45. CI also runs PHPStan level 9 with Larastan, which is a meaningful correctness signal for a package that generates SQL by string composition.
Is mavinoo/laravel-batch safe from SQL injection?
Version 3.0.0 removed an SQL injection class that existed in every 2.x release by sending all values as bound parameters instead of interpolating them into the query string. If you are still running a 2.x release, upgrading is strongly recommended before using the package with user-supplied data.
Is the instance form (new Model)->updateMultipleCondition() deprecated in laravel-batch?
Yes. As of v3.1.0 the instance form new Model()->updateMultipleCondition() is deprecated in favour of the static Model::batchUpdateMultipleCondition(), and the deprecated form is scheduled for removal in 4.0. The static HasBatch methods, including batchUpdateMultipleCondition, batchUpdateWithTwoIndex, batchUpdateByKeys, batchInsertRows, batchUpsert and batchDeleteByKeys, reduce facade coupling and improve discoverability inside your models.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
tequilarapido/trackit
gregsphotography/site-builder-filament
cabaoctavio/laravel-query-log
gregsphotography/site-builder-components
io-digital/clickatell
snowsoft/laravel-admin
oxygen/auth
relicsoftware/barakah-payment-gateway
tobya/work-with-composer
equipc/laravel-email-validator
equipc/laravel-email-database-log
adedsec/nova-translated-labels
wazza/laravel-db-encryption
hokny/laravel-generator
vonsogt/backup
jengo/queues
jeremykenedy/laravel-observability
coleus/frame
thtg88/journalism
filagora/filagora-ai-forums