mavinoo / laravelBatch

insert batch and update batch in laravel
MIT License
564 stars 118 forks source link

Static call from model #99

Closed ycs77 closed 1 year ago

ycs77 commented 1 year ago

Add HasBatch trait for model, that it can be called statically.

Usage

Add HasBatch trait into model:

namespace App\Models;

use Mavinoo\Batch\Traits\HasBatch;

class User extends Model
{
    use HasBatch;
}

Call batchUpdate() from model:

use App\Models\User;

$values = [
    [
        'id' => 1,
        'status' => 'active',
        'nickname' => 'Mohammad'
    ],
    [
        'id' => 5,
        'status' => 'deactive',
        'nickname' => 'Ghanbari'
    ],
];
$index = 'id';

User::batchUpdate($values, $index);

Call batchInsert() from model:

$columns = [
    'firstName',
    'lastName',
    'email',
    'isActive',
    'status',
];
$values = [
    [
        'Mohammad',
        'Ghanbari',
        'emailSample_1@gmail.com',
        '1',
        '0',
    ],
    [
        'Saeed',
        'Mohammadi',
        'emailSample_2@gmail.com',
        '1',
        '0',
    ],
    [
        'Avin',
        'Ghanbari',
        'emailSample_3@gmail.com',
        '1',
        '0',
    ],
];
$batchSize = 500; // insert 500 (default), 100 minimum rows in one query

User::batchInsert($columns, $values, $batchSize);