rits-tecnologia/eloquent-insert-on-conflict
Composer 安装命令:
composer require rits-tecnologia/eloquent-insert-on-conflict
包简介
Macros for INSERT ON CONFLICT with Eloquent
README 文档
README
This package provides macros to run INSERT ... ON CONFLICT DO NOTHING or DO UPDATE SET queries on models with Laravel's ORM Eloquent using PostgreSQL.
Installation
Install this package with composer.
composer require rits-tecnologia/eloquent-insert-on-conflict
If you don't use Package Auto-Discovery yet add the service provider to your Package Service Providers in config/app.php.
InsertOnConflict\InsertOnConflictServiceProvider::class,
Usage
Models
Call insertOnConflict 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::insertOnConflict($data);
Customizing the ON CONFLICT DO UPDATE clause
Update only certain columns
If you want to update only certain columns, pass them as the 2nd argument.
User::insertOnConflict([ 'id' => 1, 'name' => 'new name', 'email' => 'foo@gmail.com', ], ['name'], 'do update set', 'id'); // 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::insertOnConflict([ 'id' => 1, 'name' => 'created user', ], ['name' => 'updated user'], 'do update set', 'id');
The generated SQL is:
INSERT INTO `users` (`id`, `name`) VALUES (1, "created user") ON CONFLICT (id) DO UPDATE SET `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::insertOnConflict([ 'id' => 1, 'name' => 'created user', 'email' => 'new@gmail.com', 'password' => 'secret', ], ['name' => 'updated user', 'email'], 'do update set', 'id');
will generate
INSERT INTO `users` (`id`, `name`, `email`, `password`) VALUES (1, "created user", "new@gmail.com", "secret") ON CONFLICT (id) DO UPDATE SET `name` = "updated user", `email` = EXCLUDED.`email`
rits-tecnologia/eloquent-insert-on-conflict 适用场景与选型建议
rits-tecnologia/eloquent-insert-on-conflict 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.18k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2020 年 01 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「postgresql」 「laravel」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rits-tecnologia/eloquent-insert-on-conflict 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rits-tecnologia/eloquent-insert-on-conflict 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rits-tecnologia/eloquent-insert-on-conflict 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
SilverStripe now has tentative support for PostgreSQL ('Postgres')
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
Sql dumps containing databases for the linna apps.
统计信息
- 总下载量: 4.18k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-01-22