It seems like you're trying to resolve a dependency automatically in the constructor of a queued job, which is not how Laravel's job container resolution works. When you dispatch a job, Laravel serializes the job and its constructor arguments to store them in the queue. When the job is processed, Laravel unserializes the job and its constructor arguments.
If you want to inject a dependency into a job, you should manually resolve it from the container within the handle method of the job, not the constructor. Here's how you can modify your TestJob to work correctly:
use App\Test;
use Illuminate\Contracts\Queue\ShouldQueue;
class TestJob implements ShouldQueue
{
protected $test;
public function __construct()
{
// Don't try to resolve dependencies in the constructor of a job
}
public function handle()
{
// Resolve the dependency here instead
$this->test = resolve(Test::class);
// Now you can use $this->test as needed
}
}
And when you dispatch the job, you don't need to pass any arguments:
Route::get('/', function () {
dispatch(new \App\Jobs\TestJob());
});
This way, when the job is processed, it will resolve the Test class from the service container within the handle method, which is the correct place to resolve dependencies in queued jobs.
Remember that any data you want to pass to the job that needs to be serialized should be simple data types (like strings, integers, arrays of simple data types, etc.) and not objects that the service container needs to construct.