jhavenz/laravel-batch-update

batch updates for Eloquent models

0.0.1 2022-10-03 23:05 UTC

README

There's always been a missing bit when you want don't want to 'upsert', which Laravel covers, and you don't want to 'insert'. When you just need to 'update' all the rows, each having their own varying data set, then this package will help you out.

Requirements

- PHP: >= 8.1

Installation

You can install the package via composer:

composer require jhavenz/laravel-batch-update

Usage

Notes:

  • This package has been used a bit in my projects and is working nicely so far, but it's still in an infant and I have to write the test suite. Please lmk if you see an issue or have an idea to add.
  • I'm aware that I'm bypassing the bindings as I'm building the query, but since I'm generally using this logic when I wan to map 100s of rows, then fire the query off, I've hit limitations against the database where there's 'too many bindings' for the db engine to handle.

Thoughts?.. It's better like this, right? Unless, of course, you're passing user input to your db... in that case, I can't imagine you'd be batch updating while the user is waiting anyway.

Update multiple rows, each having their own values..and you only want to update: (not updateOrCreate, upsert, findOrCreate, etc.) e.g.

use Jhavenz\LaravelBatchUpdate\BatchedUpdate;

// 
(new BatchedUpdate(User::class))->update(
    values: [
        [
            'user_id' => 123,
            'name' => 'john doe',
            'email' => 'john@example.com',
        ],
        [
            'user_id' => 234,
            'name' => 'jane doe',
            'email' => 'jane@example.com',
        ],
        // ... a whole bunch of mapped data (in memory), then 1 query gets executed
    ],
    index: <string> /** Give an index here, or leave null and the Model's key will be used  */
    quoted: <bool> /** have you quoted the value, or should I ?..   */
);

Testing

#TODO - composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.