bmrbehnam/laravel-batch-update

为 Laravel 的批量更新

dev-master 2023-02-01 08:29 UTC

This package is auto-updated.

Last update: 2024-08-29 11:41:01 UTC


README

这是一个辅助工具,通过在模型中使用这个特性,可以添加批量更新方法。

此方法在一个查询中运行多个更新查询。

如何使用?

  1. 使用 composer 安装包

    composer require bmrbehnam/laravel-batch-update

2. 在你的模型中添加以下行

use BatchUpdate\BatchUpdateTrait;
  1. 好了!开始使用吧

     # Example data for update by one update query
     $data = [
         ['id' => 1, 'level' => 1, 'name' => 'category one'],
         ['id' => 2, 'level' => 2, 'name' => 'category two'],
         ['id' => 4, 'level' => 5, 'name' => 'category three'],
     ];
    

    批量更新

     Category::batchUpdate($data);
    
     # Or any model
     Post::batchUpdate($data);
    
    
     # Use by custom condition key by default condition key is model primary key field
    
     $data = [
         ['slug' => 'behnam', 'level' => 1, 'name' => 'category one'],
         ['slug' => 'nima', 'level' => 2, 'name' => 'category two'],
         ['slug' => 'hamed', 'level' => 5, 'name' => 'category three'],
     ];
    
     # Update level and name field in one query
     Category::batchUpdate($data,'slug');