定制 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
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固