spatie/laravel-interacts-with-payload
Inject extra data into the payload of every queued job in your Laravel app. Add keys via a facade (e.g., current user, request context), then access them inside jobs with the InteractsWithPayload trait using getFromPayload().
Pros:
InteractsWithPayload trait, ensuring minimal boilerplate for job classes. This is low-friction for adoption across existing or new jobs.AllJobs facade offers a declarative way to define payload variables (e.g., AllJobs::add('user', fn() => auth()->user())), making configuration explicit and maintainable.Cons:
auth()->user() in jobs that shouldn’t access it).SendEmailJob shouldn’t depend on a ProcessPaymentJob's payload).AllJobs::add() or getFromPayload()).auth()->user() in all jobs) could lead to tight coupling or security risks (e.g., leaking auth data in long-running jobs).$user is null or authorized).AllJobs::add('user', fn() => cache()->remember('user:payload', ...))).composer.json.PayloadTestHelper?resolve() method or job middleware? When would this package be strictly necessary?ShouldQueue, Job, and Command jobs). Works with all queue drivers (database, Redis, sync).InteractsWithPayload trait), enabling reuse across CLI workflows.spatie/laravel-package-tools. No external services or databases.// app/Providers/AppServiceProvider.php
AllJobs::add('initiator', fn() => auth()->user());
AllJobs::add('request_id', fn() => request()->header('X-Request-ID'));
getFromPayload():
use Spatie\InteractsWithPayload\Concerns\InteractsWithPayload;
class LogActivityJob implements ShouldQueue {
use InteractsWithPayload;
public function handle() {
$user = $this->getFromPayload('initiator');
// ...
}
}
ShouldQueue, Job, Command).composer require spatie/laravel-interacts-with-payload
Publish config (if needed):
php artisan vendor:publish --provider="Spatie\InteractsWithPayload\InteractsWithPayloadServiceProvider"
AppServiceProvider).AllJobs::add('tenant_id', fn() => Tenant::current()->id);
AllJobs::add('locale', fn() => app()->getLocale());
use InteractsWithPayload to job classes.getFromPayload('key') to access data.AllJobs).Log::debug('Payload:', $this->getFromPayload('*'))).AppServiceProvider), reducing boilerplate across jobs.How can I help you explore Laravel packages today?