Error 'Unresolvable dependency resolving [Parameter #0 $product_id]' when dispatching a Job with a parameter in Laravel

I am trying the “new” format to create and dispatch a job, but I am getting an error because there is a parameter in this job. Example of dispatch:

ImportProductJob::dispatch($product->id);

And an example of the class:

class ImportProductJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $tries = 3;

    protected $product_id;

    public function __construct($product_id)
    {
        $this->product_id = $product_id;
    }

    public function handle()
    { ... }

The error that is appearing is as if the parameter wasn’t passed, but I’ve already checked the value:

[2024-09-23 22:23:36] production.ERROR: Illuminate\Contracts\Container\BindingResolutionException: Unresolvable dependency resolving [Parameter #0 [ <required> $product_id ]] in class Owner\Test\Classes\Queues\ImportProductJob in /var/www/webroot/ROOT/vendor/laravel/framework/src/Illuminate/Container/Container.php:1141
Stack trace:
#0 /var/www/webroot/ROOT/vendor/laravel/framework/src/Illuminate/Container/Container.php(1050): Illuminate\Container\Container->unresolvablePrimitive(Object(ReflectionParameter))
#1 /var/www/webroot/ROOT/vendor/laravel/framework/src/Illuminate/Container/Container.php(981): Illuminate\Container\Container->resolvePrimitive(Object(ReflectionParameter))
#2 /var/www/webroot/ROOT/vendor/laravel/framework/src/Illuminate/Container/Container.php(943): Illuminate\Container\Container->resolveDependencies(Array)
#3 /var/www/webroot/ROOT/vendor/laravel/framework/src/Illuminate/Container/Container.php(795): Illuminate\Container\Container->build('Metastore\\Bling...')
#4 /var/www/webroot/ROOT/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(961): Illuminate\Container\Container->resolve('Metastore\\Bling...', Array, true)
#5 /var/www/webroot/ROOT/vendor/laravel/framework/src/Illuminate/Container/Container.php(731): Illuminate\Foundation\Application->resolve('Metastore\\Bling...', Array)
#6 /var/www/webroot/ROOT/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(946): Illuminate\Container\Container->make('Metastore\\Bling...', Array)
#7 /var/www/webroot/ROOT/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(248): Illuminate\Foundation\Application->make('Metastore\\Bling...')
#8 /var/www/webroot/ROOT/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(235): Illuminate\Queue\Jobs\Job->resolve('Metastore\\Bling...')
#9 /var/www/webroot/ROOT/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(215): Illuminate\Queue\Jobs\Job->failed(Object(Illuminate\Contracts\Container\BindingResolutionException))
#10 /var/www/webroot/ROOT/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(596): Illuminate\Queue\Jobs\Job->fail(Object(Illuminate\Contracts\Container\BindingResolutionException))
#11 /var/www/webroot/ROOT/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(542): Illuminate\Queue\Worker->failJob(Object(Illuminate\Queue\Jobs\RedisJob), Object(Illuminate\Contracts\Container\BindingResolutionException))
#12 /var/www/webroot/ROOT/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(465): Illuminate\Queue\Worker->markJobAsFailedIfWillExceedMaxAttempts('redis', Object(Illuminate\Queue\Jobs\RedisJob), 1, Object(Illuminate\Contracts\Container\BindingResolutionException))
#13 /var/www/webroot/ROOT/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(443): Illuminate\Queue\Worker->handleJobException('redis', Object(Illuminate\Queue\Jobs\RedisJob), Object(Illuminate\Queue\WorkerOptions), Object(Illuminate\Contracts\Container\BindingResolutionException))
#14 /var/www/webroot/ROOT/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(389): Illuminate\Queue\Worker->process('redis', Object(Illuminate\Queue\Jobs\RedisJob), Object(Illuminate\Queue\WorkerOptions))
#15 /var/www/webroot/ROOT/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(176): Illuminate\Queue\Worker->runJob(Object(Illuminate\Queue\Jobs\RedisJob), 'redis', Object(Illuminate\Queue\WorkerOptions))
#16 /var/www/webroot/ROOT/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(137): Illuminate\Queue\Worker->daemon('redis', 'default', Object(Illuminate\Queue\WorkerOptions))
#17 /var/www/webroot/ROOT/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(120): Illuminate\Queue\Console\WorkCommand->runWorker('redis', 'default')
#18 /var/www/webroot/ROOT/vendor/laravel/horizon/src/Console/WorkCommand.php(51): Illuminate\Queue\Console\WorkCommand->handle()
#19 /var/www/webroot/ROOT/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): Laravel\Horizon\Console\WorkCommand->handle()
#20 /var/www/webroot/ROOT/vendor/laravel/framework/src/Illuminate/Container/Util.php(41): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#21 /var/www/webroot/ROOT/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(93): Illuminate\Container\Util::unwrapIfClosure(Object(Closure))
#22 /var/www/webroot/ROOT/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(35): Illuminate\Container\BoundMethod::callBoundMethod(Object(October\Rain\Foundation\Application), Array, Object(Closure))
#23 /var/www/webroot/ROOT/vendor/laravel/framework/src/Illuminate/Container/Container.php(662): Illuminate\Container\BoundMethod::call(Object(October\Rain\Foundation\Application), Array, Array, NULL)
#24 /var/www/webroot/ROOT/vendor/laravel/framework/src/Illuminate/Console/Command.php(211): Illuminate\Container\Container->call(Array)
#25 /var/www/webroot/ROOT/vendor/symfony/console/Command/Command.php(326): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#26 /var/www/webroot/ROOT/vendor/laravel/framework/src/Illuminate/Console/Command.php(180): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#27 /var/www/webroot/ROOT/vendor/symfony/console/Application.php(1096): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#28 /var/www/webroot/ROOT/vendor/symfony/console/Application.php(324): Symfony\Component\Console\Application->doRunCommand(Object(Laravel\Horizon\Console\WorkCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#29 /var/www/webroot/ROOT/vendor/symfony/console/Application.php(175): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#30 /var/www/webroot/ROOT/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(201): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#31 /var/www/webroot/ROOT/artisan(33): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#32 {main}

Hi! Pretty sure you need to use the model in the dispatch call, and type hint your model in the Job constructor. Laravel will then handle the serialization of your model.

ImportProductJob::dispatch($product);

class ImportProductJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $tries = 3;

    protected Product $product;

    public function __construct(Product $product)
    {
        $this->product = $product;
    }

   ....

And if I want to pass values ​​that are not a model, would it be possible?

Pretty sure that you can still pass a string or array to the job handler, you just have to type hint it in the constructor.
But I don’t have a testable example at hand and don’t have in-depth knowledge - and it also depends on the queue driver.

Try it out and don’t forget to declare / type-hint your parameters and member variables!