Git Product home page Git Product logo

insert-update-many's Introduction

Laravel Eloquent's Insert/Update Many

Laravel's batch insert or batch update for collection of eloquent models. Perform single query for batch insert or update operations. This updates the created_at and updated_at column of the models and the tables. The method names insertMany and updateMany is based from the eloquent method name saveMany. Both insertMany and updateMany can accept collection of models or just plain array data.

Installation

composer require ajcastro/insert-update-many

Usage

Insert Many

Directly pass array or collection of models unlike Laravel's built-in insert() which only accept arrays. This already sets the created_at and updated_at columns.

$users = factory(User::class, 10)->make();
User::insertMany($users);

How it works

The passed collection of models is transformed to its array form, only including fillable attributes, and passed its array form to Laravel's native insert() method.

Update Many

Update array or collection of models. This perform a single update query for all the passed models. Only the dirty or changed attributes will be included in the update. This updates the updated_at column of the models and the tables.

User::updateMany($users); // update many models using id as the default key
User::updateMany($users, 'id'); // same as above
User::updateMany($users, 'username'); // use username as key instead of id

Specifying which columns to be updated

User::updateMany($users, 'id', ['email', 'first_name', 'last_name']);

How it works

This will produce a query like this:

UPDATE
   `users`
SET
   `email` =
   CASE
      WHEN
         `id` = '426'
      THEN
         '[email protected]'
      WHEN
         `id` = '427'
      THEN
         '[email protected]'
      WHEN
         `id` = '428'
      THEN
         '[email protected]'
      ELSE
         `email`
   END
, `first_name` =
   CASE
      WHEN
         `id` = '426'
      THEN
         'Orie'
      WHEN
         `id` = '427'
      THEN
         'Hubert'
      WHEN
         `id` = '428'
      THEN
         'Mikayla'
      ELSE
         `first_name`
   END
, `last_name` =
   CASE
      WHEN
         `id` = '426'
      THEN
         'Weissnat'
      WHEN
         `id` = '427'
      THEN
         'Wiza'
      WHEN
         `id` = '428'
      THEN
         'Keeling'
      ELSE
         `last_name`
   END
WHERE
   `id` IN
   (
      426, 427, 428
   );

insert-update-many's People

Contributors

ajcastro avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

bunix

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.