PHP code example of mavinoo / laravel-batch
1. Go to this page and download the library: Download mavinoo/laravel-batch library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mavinoo / laravel-batch example snippets
use App\Models\User;
$userInstance = new User;
$arrays = [
[
'conditions' => ['id' => 1, 'status' => 'active'],
'columns' => [
'status' => 'invalid',
'nickname' => 'mohammad',
],
],
[
'conditions' => ['id' => 2],
'columns' => [
'nickname' => 'mavinoo',
'name' => 'mohammad',
],
],
[
'conditions' => ['id' => 3],
'columns' => [
'nickname' => 'ali',
],
],
];
$keyName = 'id';
Batch::updateMultipleCondition($userInstance, $arrays, $keyName);
// or
batch()->updateMultipleCondition($userInstance, $arrays, $keyName);
use App\Models\User;
$userInstance = new User;
$value = [
[
'id' => 1,
'status' => 'active',
'nickname' => 'Mohammad',
],
[
'id' => 5,
'status' => 'deactive',
'nickname' => 'Ghanbari',
],
];
$index = 'id';
Batch::update($userInstance, $value, $index);
// or
batch()->update($userInstance, $values, $index);
use App\Models\User;
$userInstance = new User;
$value = [
[
'id' => 1,
'status' => 'active',
],
[
'id' => 5,
'status' => 'deactive',
'nickname' => 'Ghanbari',
],
[
'id' => 10,
'status' => 'active',
'date' => Carbon::now(),
],
[
'id' => 11,
'username' => 'mavinoo',
],
];
$index = 'id';
Batch::update($userInstance, $value, $index);
// or
batch()->update($userInstance, $values, $index);
use App\Models\User;
$userInstance = new User;
$value = [
[
'id' => 1,
'balance' => ['+', 500], // Add
],
[
'id' => 2,
'balance' => ['-', 200], // Subtract
],
[
'id' => 3,
'balance' => ['*', 5], // Multiply
],
[
'id' => 4,
'balance' => ['/', 2], // Divide
],
[
'id' => 5,
'balance' => ['%', 2], // Modulo
],
];
$index = 'id';
Batch::update($userInstance, $value, $index);
// or
batch()->update($userInstance, $values, $index);
use App\Models\User;
$userInstance = new User;
$columns = [
'firstName',
'lastName',
'email',
'isActive',
'status',
];
$values = [
[
'Mohammad',
'Ghanbari',
'[email protected] ',
'1',
'0',
],
[
'Saeed',
'Mohammadi',
'[email protected] ',
'1',
'0',
],
[
'Avin',
'Ghanbari',
'[email protected] ',
'1',
'0',
],
];
$batchSize = 500; // insert 500 (default), 100 minimum rows in one query
$result = Batch::insert($userInstance, $columns, $values, $batchSize);
// or
$result = batch()->insert($userInstance, $values, $index);
// result: false or array
sample array result:
Array
(
[totalRows] => 384
[totalBatch] => 500
[totalQuery] => 1
)
namespace App\Models;
use Mavinoo\Batch\Traits\HasBatch;
class User extends Model
{
use HasBatch;
}
use App\Models\User;
// ex: update
User::batchUpdate($value, $index);
// ex: insert
User::batchInsert($columns, $values, $batchSize);
// ex: update
$result = batch()->update($userInstance, $value, $index);
// ex: insert
$result = batch()->insert($userInstance, $columns, $values, $batchSize);