- How do I inject the authenticated user into every queued job in Laravel?
- Use the `AllJobs` facade in a service provider to define the payload variable: `AllJobs::add('user', fn() => auth()->user());`. Then, access it in any job class using the `InteractsWithPayload` trait via `$this->getFromPayload('user')`. This avoids repeating auth logic across jobs.
- Does this package work with Laravel 8, 9, or 10? Are there breaking changes?
- The package is tested with recent Laravel versions (8+ as of 2025). Check the [release notes](https://github.com/spatie/laravel-interacts-with-payload/releases) for breaking changes. Pin your `composer.json` version to avoid surprises during updates. Legacy apps (pre-8) may need adjustments.
- Can I inject different payloads for different job types (e.g., tenant ID for some jobs, not others)?
- No, this package injects the same payload globally to all jobs. For job-specific payloads, consider using Laravel’s `resolve()` method in job constructors or custom middleware. This package is best for truly shared context (e.g., auth user, request data).
- How do I mock payload data in PHPUnit tests for jobs?
- Stub the `AllJobs` facade to return mock data: `AllJobs::shouldReceive('add')->once();` or override payloads in tests using `AllJobs::setPayload('user', $mockUser)`. Test job classes in isolation to avoid flaky shared-state tests. Use dependency injection for complex scenarios.
- Will injecting payloads slow down job processing if the data is expensive to compute (e.g., DB queries)?
- Yes, expensive payload computations (e.g., `auth()->user()`) run for every job. Mitigate this by caching payloads: `AllJobs::add('user', fn() => cache()->remember('user:payload', ...))`. Avoid real-time data unless necessary, or use lazy-loading in jobs.
- Is there a way to validate or sanitize payload data before it’s injected into jobs?
- No built-in validation exists, but you can pre-process payloads in the `add()` closure (e.g., `AllJobs::add('user', fn() => auth()->check() ? auth()->user() : null)`). Always validate payloads in jobs (e.g., `if (!$this->getFromPayload('user')) { abort(403); }`) to handle missing or invalid data.
- How does this compare to using Laravel’s `resolve()` method or job middleware for payloads?
- This package is simpler for global payloads (e.g., auth user) as it centralizes logic in one place. `resolve()` is better for job-specific dependencies, while middleware is ideal for pre/post-processing. Use this package only if you need shared context across *all* jobs.
- Can I use this package with Laravel Horizon or other queue drivers (Redis, database)?
- Yes, the package works seamlessly with all Laravel queue drivers, including Horizon. Payloads are serialized with the job and remain consistent regardless of the driver. No additional configuration is needed for Redis or database queues.
- What happens if a payload variable fails to resolve (e.g., `auth()->user()` returns null)?
- The payload variable will be `null` in the job. Handle this gracefully in your job’s `handle()` method (e.g., `if (!$user = $this->getFromPayload('user')) { return; }`). Avoid relying on payloads for critical logic unless you validate them explicitly.
- How do I debug or log payload data in failed jobs (e.g., for error tracking)?
- Extend your job’s `failed()` method to log payloads: `Log::error('Job failed with payload:', ['user' => $this->getFromPayload('user')])`. For failed jobs, use Laravel’s `failed_jobs` table or tools like Sentry to inspect payloads. Consider serializing payloads to JSON for clarity.