PHPackages                             mosamirzz/bulk-query - 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. [Database &amp; ORM](/categories/database)
4. /
5. mosamirzz/bulk-query

ActiveLibrary[Database &amp; ORM](/categories/database)

mosamirzz/bulk-query
====================

Perform bulk update/insert/delete with laravel.

2.1(3y ago)117.8k1[1 PRs](https://github.com/mohamed-samir907/bulk-query/pulls)MITPHPPHP ^8.1

Since Oct 1Pushed 3y ago1 watchersCompare

[ Source](https://github.com/mohamed-samir907/bulk-query)[ Packagist](https://packagist.org/packages/mosamirzz/bulk-query)[ Docs](https://github.com/mosamirzz/laravel-bulk-query)[ RSS](/packages/mosamirzz-bulk-query/feed)WikiDiscussions main Synced today

READMEChangelog (6)Dependencies (3)Versions (8)Used By (0)

Bulk Query
==========

[](#bulk-query)

[![Latest Version on Packagist](https://camo.githubusercontent.com/b3d8327cf0cefe038932e2744f0dc1e7e02f26fbe0ea933bc50752b0eafb54c3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d6f73616d69727a7a2f62756c6b2d71756572792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mosamirzz/bulk-query)[![Total Downloads](https://camo.githubusercontent.com/fcba7eee0e54626abd17949cf75d74bfa9e85da82021041d6648d6ff39fe79d0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d6f73616d69727a7a2f62756c6b2d71756572792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mosamirzz/bulk-query)

Perform Bulk/Batch Update/Insert/Delete with laravel.

Problem
-------

[](#problem)

I tried to make bulk update with laravel but i found that Laravel doesn't support that; so i created this package through it you can perform Batch/Bulk Update, Insert and delete.

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

[](#installation)

You can install the package via composer:

```
composer require mosamirzz/bulk-query
```

Usage
-----

[](#usage)

> Bulk or batch update meaning: that you can update multiple records in single query. Same for delete and insert (can insert or delete multiple records in single query).

The package provided you with 4 classes:

- `Insert::class` Used to insert multiple records in single query.
- `Update::class` Used to update multiple records in single query.
- `InsertOrUpdate::class` Used to insert multiple records in single query, and when duplicate record found in the records it will update it.
    - This query updates the records only when it find a `unique` or `primary key`. meaning that if the table doesn't have `unique` or `primary key` column and tried to insert record twice it will inserted as 2 records and will not update the record.
    - note: the `primary key` sholud sent to the query to determine by it if the query will update the duplicate records or not. if it will not send and `unique` column not sent to the query then the query will perfom insert only.
- `Delete::class` Used to delete multiple records in single query.

### Bulk Delete:

[](#bulk-delete)

You can perfrom Bulk or batch delete by using the class `Delete::class` as following:

```
use Mosamirzz\BulkQuery\Delete;

// 1. create new instance and pass the table name to the constructor.
$delete = new Delete("users");
// 2. send the `IDs` of the records to the prepare as array.
$delete->prepare([1, 2, 3, 4]);
// 3. execute the bulk delete query.
$delete->execute();
```

As shown above the default of delete statment is delete by the column `id` but if you want to perform the delete query with different column you can use the method `useKey` and pass to it the column name as following:

```
use Mosamirzz\BulkQuery\Delete;

$delete = new Delete("users");
// change the default column used by the delete statment.
$delete->useKey("email");
// pass the values of the key that we want to delete it's records.
$delete->prepare(["gm.mohamedsamir@gmail.com", "user1@test.com", "user2@test.com"]);
$delete->execute();
```

> The default `key` used in the delete is the column `id`

### Bulk Insert:

[](#bulk-insert)

You can perform bulk insert by using the class `Insert::class` as following:

```
use Mosamirzz\BulkQuery\Insert;

// 1. create new instance and pass the table name.
$insert = new Insert("users");
// 2. pass the columns used by the insert query.
$insert->useColumns(["name", "email", "password"]);
// 3. send the records that we want to insert.
$insert->prepare([
    [
        "name" => "mohamed samir",
        "email" => "gm.mohamedsamir@gmail.com",
        "password" => "123456"
    ],
    [
        "name" => "user 1",
        "email" => "user1@test.com",
        "password" => "123456"
    ],
]);
// 4. execute the bulk insert query.
$insert->execute();
```

As shown above the `prepare` method accepts `array[]` each array represents the record that we want to insert in the database.

If you are trying to send record that exists before in the table, then the query will throw an exception like the following: `SQLSTATE[23000]: Integrity constraint violation`

### Bulk Update:

[](#bulk-update)

You can perform bulk update by using the class `Update::class` as following:

```
use Mosamirzz\BulkQuery\Update;

// 1. create instance and pass the table name.
$update = new Update("users");
// 2. pass the columns that we need to update them.
$update->useColumns(["name", "password"]);
// 3. pass the records that we need to update them
$update->prepare([
    1001 => [
        "name" => "mohamed samir updated",
        "password" => "1234_updated"
    ],
    1105 => [
        "name" => "user updated",
        "password" => "4321_updated"
    ],
]);
// 4. execute the bulk update query.
$update->execute();
```

As shown above

- the `prepare` method accepts `array[]` each array represents the record that we want to update in the database.
- each record must be like `"key" => [...]` the `key` represent the column name we use to updatte the record. in the example above the key it's default is `id` so when we want to update the record we must send the record `id` as the `key` and `array` of values that we want to update.

You can change the default column `id` used by the update to another column with `useKey` method.

for example you can use the `email` column as the key used by update as the following:

```
use Mosamirzz\BulkQuery\Update;

$update = new Update("users");
$update->useColumns(["name", "password"]);
// change the default key used by the update.
$update->useKey("email");
$update->prepare([
    "gm.mohamedsamir@gmail.com" => [
        "name" => "mohamed samir updated",
        "password" => "mohamed"
    ],
    "user1@gmail.com" => [
        "name" => "user 1",
        "password" => "123456"
    ],
]);
$update->execute();
```

As shown above we used `email` column as the key used in update statement.

> The default `key` used in the update is the column `id`

If you have a record that you want to update only one column and set the other columns values to old value in the database then you can remove the `key=>value` of the column from the array as following:

```
$update = new Update("users");
$update->useColumns(["name", "password", "is_admin"]);
$update->prepare([
    1 => [
        "is_admin" => true,
    ],
    3 => [
        "name" => "ahmed",
        "is_admin" => false,
    ],
    70 => [
        "password" => "123123"
    ],
]);
$update->execute();
```

As shown above the user with `id` = 1 will update the column `is_admin` only, we don't need to pass the `name` and `password` columns to it, the query will put the old value of `name` and `password` for the user with `id` = 1.

Same for the user with `id` = 3 the columns that will be updated is `name` and `is_admin` but the `password` column will be the old value.

Also the user with `id` = 70 only `password` will be updated.

### Bulk Insert or Update:

[](#bulk-insert-or-update)

You can perform bulk insert and update in the same query by using the class `InsertOrUpdate::class`.

> The query updates the record only when it find a `unique` column or `primary key` column.

You can use the class as the following:

```
use Mosamirzz\BulkQuery\InsertOrUpdate;

// 1. create instance and pass the table name.
$query = new InsertOrUpdate("users");
// 2. pass the columns used in insertion.
$query->useColumns(["name", "email", "password"]);
// 3. pass the columns used in the update when it find duplicate record.
$query->updatableColumns(["name", "password"]);
// 4. pass the records that we need to insert or update.
$query->prepare([
    [
        "name" => "mohamed samir",
        "email" => "gm.mohamedsamir@gmail.com",
        "password" => "mohamed124"
    ],
    [
        "name" => "hello",
        "email" => "hello@user.com",
        "password" => "hello"
    ],
]);
// 5. execute the bulk insert or update query.
$query->execute();
```

As shown above

- suppose that the user with `email` = `gm.mohamedsamir@gmail.com` is exists in the table before.
- So when the query executes the record that has `email` = `gm.mohamedsamir@gmail.com` will be updated but the only columns `name` and `password` are updated.
- The second record that has `email` = `hello@user.com` will be inserted to the table because it's not exists before.

Testing
-------

[](#testing)

```
composer run test
```

Changelog
---------

[](#changelog)

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

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

[](#contributing)

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

Security
--------

[](#security)

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

Credits
-------

[](#credits)

- [Mohamed Samir](https://github.com/mosamirzz)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

Laravel Package Boilerplate
---------------------------

[](#laravel-package-boilerplate)

This package was generated using the [Laravel Package Boilerplate](https://laravelpackageboilerplate.com).

###  Health Score

33

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity64

Established project with proven stability

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

Recently: every ~138 days

Total

6

Last Release

1180d ago

Major Versions

1.0.2 → 2.02023-04-10

PHP version history (3 changes)1.0PHP ^7.4|^8.0

2.0PHP ^8.0

2.1PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/b8d2c594fd2fc1ff26195258817c14019d49061464450e46ec58788ed9728469?d=identicon)[samirzz](/maintainers/samirzz)

---

Top Contributors

[![mohamed-samir907](https://avatars.githubusercontent.com/u/40417075?v=4)](https://github.com/mohamed-samir907 "mohamed-samir907 (9 commits)")

---

Tags

laraveldeletebulk insertbulk updatemosamirzzbulk deletebulk-querybatch insertbatch updatebatch delete

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mosamirzz-bulk-query/health.svg)

```
[![Health](https://phpackages.com/badges/mosamirzz-bulk-query/health.svg)](https://phpackages.com/packages/mosamirzz-bulk-query)
```

###  Alternatives

[illuminate/database

The Illuminate Database package.

2.8k54.9M11.6k](/packages/illuminate-database)[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k8.4M95](/packages/mongodb-laravel-mongodb)[kirschbaum-development/eloquent-power-joins

The Laravel magic applied to joins.

1.6k32.6M46](/packages/kirschbaum-development-eloquent-power-joins)[yajra/laravel-oci8

Oracle DB driver for Laravel via OCI8

8793.2M25](/packages/yajra-laravel-oci8)[glushkovds/phpclickhouse-laravel

Adapter of the most popular library https://github.com/smi2/phpClickHouse to Laravel

2051.5M2](/packages/glushkovds-phpclickhouse-laravel)[vectorial1024/laravel-cache-evict

Efficiently remove expired Laravel file/database cache data

5917.4k](/packages/vectorial1024-laravel-cache-evict)

PHPackages © 2026

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