viktorruskai/advanced-upsert-for-laravel

本包最新版本(1.0.1)没有提供许可证信息。

本包为Laravel提供了高级upsert功能。

1.0.1 2022-03-29 19:34 UTC

This package is auto-updated.

Last update: 2024-08-29 06:01:52 UTC


README

GitHub release (latest by date) PHPUnit PHPStan License

目前该功能仅适用于PostgreSQL(在您的.env文件中,DB_CONNECTION必须设置为pgsql)。根据维基词典,upsert是一种操作,如果数据库表中不存在行,则将其插入,如果存在则更新。高级upsert基本上与Laravel的upsert函数相同,但它有一个关键优势。它可以在执行upsert时获取外键(id)。

⚡️️ 安装

$ composer require viktorruskai/advanced-upsert-for-laravel

⚙️ 使用

  1. 在您的Laravel Eloquent模型中添加use HasUpsert;(确保您有正确的命名空间)
  2. 您可以使用两种方式使用它
    • 常规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 
      );
      生成的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
      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) 
      );
      生成的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"

🌍 示例

请查看tests/Support/Tests/文件夹中的更多示例。

⚖️ 许可证

本包的内容是开源代码,许可协议为MIT许可证。