1. Go to this page and download the library: Download schoolpalm/queued-jobs 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/ */
schoolpalm / queued-jobs example snippets
namespace App\Jobs;
use SchoolPalm\QueuedJobs\Jobs\ContextAwareJob;
class GenerateReport extends ContextAwareJob
{
public function __construct(
private readonly array $reportData
) {}
public function handle(): void
{
// Context is automatically available
$context = $this->getQueueContext();
$schoolId = $context['school_id'];
$userId = $context['user_id'];
$module = $context['module'];
// ... your job logic
}
}
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use SchoolPalm\QueuedJobs\Facades\QueuedJobs;
class AppServiceProvider extends ServiceProvider
{
public function boot(): void
{
QueuedJobs::resolveContextUsing(function () {
return [
'tenant_id' => tenancy()->tenant?->id,
'school_id' => session('school_id'),
'user_id' => auth()->id(),
'module' => request()->route('module'),
'metadata' => [
'ip' => request()->ip(),
],
];
});
}
}
QueuedJobs::restoreContextUsing(function (QueueContext $context) {
// Switch tenant database
if ($context->tenantId()) {
tenancy()->initialize($context->tenantId());
}
// Set current school
if ($context->schoolId()) {
session(['school_id' => $context->schoolId()]);
}
// Authenticate user
if ($context->userId()) {
auth()->loginUsingId($context->userId());
}
});
use SchoolPalm\QueuedJobs\Facades\QueuedJobs;
// Basic dispatch — context is captured automatically
QueuedJobs::job(new GenerateReport($data))->dispatch();
// With fluent context overrides
QueuedJobs::job(new GenerateReport($data))
->withTenant(1)
->withSchool(20)
->withUser(50)
->withModule('reports')
->withMetadata(['source' => 'api'])
->onQueue('high')
->onConnection('redis')
->delay(now()->addMinutes(5))
->dispatch();
$builder = QueuedJobs::job(new GenerateReport($data))
->withSchool(10)
->dispatch();
// Access the created result directly after dispatch
$result = $builder->result(); // QueueJobResult model
$resource = $builder->resultResource(); // JobResultResource
$array = $builder->resultArray(); // API-friendly array