guidocella/eloquent-insert-on-duplicate-key
最新稳定版本:v2.2.5
Composer 安装命令:
composer require guidocella/eloquent-insert-on-duplicate-key
包简介
Macros for INSERT ON DUPLICATE KEY UPDATE and INSERT IGNORE with Eloquent
README 文档
README
This package is deprecated because upsert and insertOrIgnore have been added to Laravel.
If you need the pivot functions, they are trivial to implement:
BelongsToMany::macro('attachUpsert', function ($id, array $attributes = []) { $this->newPivotStatement()->upsert($this->formatAttachRecords( $this->parseIds($id), $attributes ), null); }); BelongsToMany::macro('attachOrIgnore', function ($id, array $attributes = []) { $this->newPivotStatement()->insertOrIgnore($this->formatAttachRecords( $this->parseIds($id), $attributes )); });
This package provides macros to run INSERT ... ON DUPLICATE KEY UPDATE and INSERT IGNORE queries on models and pivot tables with Laravel's ORM Eloquent using MySql or MariaDB.
Installation
Install this package with composer.
composer require guidocella/eloquent-insert-on-duplicate-key
If you don't use Package Auto-Discovery yet add the service provider to your Package Service Providers in config/app.php.
InsertOnDuplicateKey\InsertOnDuplicateKeyServiceProvider::class,
Usage
Models
Call insertOnDuplicateKey or insertIgnore from a model with the array of data to insert in its table.
$data = [ ['id' => 1, 'name' => 'name1', 'email' => 'user1@email.com'], ['id' => 2, 'name' => 'name2', 'email' => 'user2@email.com'], ]; User::insertOnDuplicateKey($data); User::insertIgnore($data);
Customizing the ON DUPLICATE KEY UPDATE clause
Update only certain columns
If you want to update only certain columns, pass them as the 2nd argument.
User::insertOnDuplicateKey([ 'id' => 1, 'name' => 'new name', 'email' => 'foo@gmail.com', ], ['name']); // The name will be updated but not the email.
Update with custom values
You can customize the value with which the columns will be updated when a row already exists by passing an associative array.
In the following example, if a user with id = 1 doesn't exist, it will be created with name = 'created user'. If it already exists, it will be updated with name = 'updated user'.
User::insertOnDuplicateKey([ 'id' => 1, 'name' => 'created user', ], ['name' => 'updated user']);
The generated SQL is:
INSERT INTO `users` (`id`, `name`) VALUES (1, "created user") ON DUPLICATE KEY UPDATE `name` = "updated user"
You may combine key/value pairs and column names in the 2nd argument to specify the columns to update with a custom literal or expression or with the default VALUES(column). For example:
User::insertOnDuplicateKey([ 'id' => 1, 'name' => 'created user', 'email' => 'new@gmail.com', 'password' => 'secret', ], ['name' => 'updated user', 'email']);
will generate
INSERT INTO `users` (`id`, `name`, `email`, `password`) VALUES (1, "created user", "new@gmail.com", "secret") ON DUPLICATE KEY UPDATE `name` = "updated user", `email` = VALUES(`email`)
Pivot tables
Call attachOnDuplicateKey and attachIgnore from a BelongsToMany relation to run the inserts in its pivot table. You can pass the data in any of the formats accepted by attach.
$pivotData = [ 1 => ['expires_at' => Carbon::today()], 2 => ['expires_at' => Carbon::tomorrow()], ]; $user->roles()->attachOnDuplicateKey($pivotData); $user->roles()->attachIgnore($pivotData);
guidocella/eloquent-insert-on-duplicate-key 适用场景与选型建议
guidocella/eloquent-insert-on-duplicate-key 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 583.47k 次下载、GitHub Stars 达 87, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「mysql」 「laravel」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 guidocella/eloquent-insert-on-duplicate-key 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 guidocella/eloquent-insert-on-duplicate-key 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 guidocella/eloquent-insert-on-duplicate-key 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Store your language lines in the database, yaml or other sources
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
A PSR-7 compatible library for making CRUD API endpoints
PHP module for MySql database
VV database abstraction layer with query builder and DB structure models
统计信息
- 总下载量: 583.47k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 88
- 点击次数: 35
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 未知