viktorruskai/advanced-upsert-for-laravel
Composer 安装命令:
composer require viktorruskai/advanced-upsert-for-laravel
包简介
This package contains advanced upsert functionality for Laravel.
README 文档
README
This feature is for now only available for PostgreSQL (in your .env file DB_CONNECTION must be set to pgsql).
Upsert, based on Wiktionary, is an operation that inserts rows into a database table if they do not already exist, or updates them if they do.
The Advanced upsert is basically the same as Laravel's upsert function, but it has one key advantage. It can fetch foreign key (id) when performing an upsert.
⚡️️ Installation
$ composer require viktorruskai/advanced-upsert-for-laravel
⚙️ Usage
- Add
use HasUpsert;in your Laravel Eloquent model (make sure you have correct namespace) - You can use it in two ways:
- Normal upsert
ItemAction::upsert( [ [ 'itemId' => 1, 'actionName' => 'Purchased', 'actionDescription' => 'Test description', 'actionValue' => 12, ], // ... (more items) ], ['itemId', 'actionName'], // Conflict (either columns or key name) ['actionDescription'] // Update column );
Generated SQL:INSERT INTO "itemActions" ("itemId", "actionName", "actionDescription", "actionValue", "updatedAt", "createdAt") VALUES (1, 'Purchased', 'Test description', 12, NOW(), NOW()) /** ... (more items) */ ON CONFLICT ("itemId", "actionName") DO UPDATE SET "actionDescription" = "excluded"."actionDescription"
- Upsert with selecting foreign ID from different table
ItemActionAdditional::upsert( [ [ 'where' => [ 'itemId' => 1, 'actionName' => 'Test', ], 'upsert' => [ 'itemActionId' => '*' // Must be set `*`, this ID will be automatically added from `$selectModelClassName` by conditions from `where` param 'specialData' => '123456', 'description' => 'Hello', ], ], // ... (more items) ], ['itemActionId', 'specialData'], // Must be set as unique key (name of columns must be presented or name of the key) ['description'], // Columns that will be updated ItemAction::class, // Eloquent model, in this case must be set [...] // Any columns that should be returned (Not required) );
Generated SQL:INSERT INTO "itemActionAdditional" ("itemActionId", "specialData", "description", "updatedAt", "createdAt") ( SELECT id, '123456', 'Hello', NOW(), NOW() FROM "itemActions" WHERE "itemId" = 1 AND "actionName" = 'Test' ) /** ... (more items) */ ON CONFLICT ("itemActionId", "specialData") DO UPDATE SET "description" = "excluded"."description"
- Normal upsert
🌍 Examples
Check the tests/Support/Tests/ folder for more examples.
⚖️ Licence
Content of this package is open-sourced code licensed under the MIT license.
viktorruskai/advanced-upsert-for-laravel 适用场景与选型建议
viktorruskai/advanced-upsert-for-laravel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 03 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 viktorruskai/advanced-upsert-for-laravel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 viktorruskai/advanced-upsert-for-laravel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 5
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2022-03-21