PHP code example of smartgeomatics / laravel-deferred-batch
1. Go to this page and download the library: Download smartgeomatics/laravel-deferred-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/ */
smartgeomatics / laravel-deferred-batch example snippets
use Illuminate\Support\Facades\Bus;
use Illuminate\Bus\PendingBatch;
use SmartGeomatics\DeferredBatch\DeferredBatch;
Bus::chain([
new PrepareDataJob,
new DeferredBatch(function () {
$items = Item::where('status', 'pending')->get();
if ($items->isEmpty()) {
return null; // skip batch, continue chain
}
return Bus::batch(
$items->map(fn ($item) => new ProcessItemJob($item))
)->name('Process pending items');
}),
new FinalizeJob,
])->dispatch();
new DeferredBatch(function () {
if (! $this->shouldRunBatch()) {
return null;
}
return Bus::batch([...]);
})
class BuildReportBatch
{
public function __invoke(): ?PendingBatch
{
$reports = Report::whereNull('generated_at')->get();
if ($reports->isEmpty()) {
return null;
}
return Bus::batch(
$reports->map(fn ($report) => new GenerateReportJob($report))
);
}
}
Bus::chain([
new DeferredBatch(new BuildReportBatch),
new SendReportNotificationJob,
])->dispatch();
Bus::chain([
(new DeferredBatch(function () {
return Bus::batch([new SomeJob]);
}))->onQueue('high')->onConnection('redis'),
new NextJob,
])->dispatch();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.