承接 ajcastro/insert-update-many 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ajcastro/insert-update-many

最新稳定版本:v0.1.0

Composer 安装命令:

composer require ajcastro/insert-update-many

包简介

Laravel's batch insert or batch update for collection of eloquent models.

README 文档

README

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
         'favian.russel@example.com'
      WHEN
         `id` = '427'
      THEN
         'opurdy@example.org'
      WHEN
         `id` = '428'
      THEN
         'kaylah.hyatt@example.com'
      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
   );

统计信息

  • 总下载量: 1.69k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-07-12