1. Go to this page and download the library: Download wfeller/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/ */
wfeller / laravel-batch example snippets
use App\Models\User;
use WF\Batch\Batch;
// Save multiple models at once
$users = [
['name' => 'John', 'email' => '[email protected]'],
['name' => 'Jane', 'email' => '[email protected]'],
$existingUser // existing model instance
];
$userIds = Batch::of(User::class, $users)->save()->now();
use App\Models\Car;
use WF\Batch\Batch;
$cars = [
['brand' => 'Audi', 'model' => 'A6'],
['brand' => 'Ford', 'model' => 'Mustang'],
$existingCar // existing model instance
];
// Save immediately
$carIds = Batch::of(Car::class, $cars)->save()->now();
// Set custom batch size
$carIds = Batch::of(Car::class, $cars)->batchSize(100)->save()->now();
use App\Models\User;
use WF\Batch\Batch;
$users = [
['id' => 1, 'name' => 'Updated John'],
['id' => 2, 'name' => 'Updated Jane'],
$userInstance // existing model with changes
];
$updatedIds = Batch::of(User::class, $users)->save()->now();
use App\Models\Car;
use WF\Batch\Batch;
// Delete by IDs
$carIds = [1, 2, 3, 5, 8];
$deletedIds = Batch::of(Car::class, $carIds)->delete()->now();
// Delete by model instances
$cars = Car::find([1, 2, 3]);
$deletedIds = Batch::of(Car::class, $cars)->delete()->now();
// Mixed approach
$mixed = [1, $carInstance, 3, $anotherCar];
$deletedIds = Batch::of(Car::class, $mixed)->delete()->now();
use App\Models\User;
use WF\Batch\Batch;
$users = [
['name' => 'John', 'email' => '[email protected]'],
['name' => 'Jane', 'email' => '[email protected]']
];
// Dispatch to default queue
Batch::of(User::class, $users)->save()->dispatch();
// Dispatch to specific queue
Batch::of(User::class, $users)->save()->onQueue('high-priority')->dispatch();
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use WF\Batch\Traits\Batchable;
class Car extends Model
{
use Batchable;
// ...
}
// Now you can use batch operations directly on the model
$cars = [
['brand' => 'Audi', 'model' => 'A6'],
['brand' => 'Ford', 'model' => 'Mustang']
];
// These are equivalent
$carIds = Car::newBatch($cars)->save()->now();
$carIds = Car::batchSave($cars);
// For deletion
Car::batchDelete([1, 2, 3]);
use App\Models\User;
use WF\Batch\Batch;
$users = collect()->range(1, 10000)->map(fn($i) => [
'name' => "User $i",
'email' => "[email protected]"
]);
// Process in batches of 500 (default)
Batch::of(User::class, $users)->save()->now();
// Process in batches of 1000
Batch::of(User::class, $users)->batchSize(1000)->save()->now();
// Set global default batch size
Batch::setDefaultBatchSize(1000);
// Fastest but no events
User::insert([$userA, $userB, $userC]);
// Balanced: good performance with events
Batch::of(User::class, [$userA, $userB, $userC])->save()->now();
// Slowest: individual operations
foreach ([$userA, $userB, $userC] as $user) {
User::create($user);
}
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
protected static function booted()
{
static::creating(function (User $user) {
// Called for each new user in batch
$user->created_by = auth()->id();
});
static::updating(function (User $user) {
// Called for each updated user in batch
$user->updated_by = auth()->id();
});
static::deleting(function (User $user) {
// Called for each user being deleted in batch
$user->deleted_by = auth()->id();
});
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.