Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do I Pass Parameters to a Queue?

I am using Laravel 5.5 to create a simple queue that will resize an uploaded image. I cannot figure out how to pass parameters to the queue, such as the temporary file location of the image. My colleagues have used older versions of Laravel where a string could be passed to a job eg:

$this->dispatch(new ExampleJob($foo));

However, in Laravel 5.5 passing a string in the same way, eg:

ExampleJob::dispatch($foo);

will trigger a 'BindingResolutionException' exception as it's expecting a model to serialize.

If that's the case, how do I pass anything else in?

like image 878
Captain flapjack Avatar asked Nov 07 '22 14:11

Captain flapjack


1 Answers

Something Like this works for me in Laravel 5.7 and Laravel 5.5...

ExampleJob::dispatch($foo);

Then the class and constructor look like this...

<?php

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

    protected $foo;

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

    public function handle()
    {
        $bar = $this->foo;
    }
}
like image 50
Frank Avatar answered Nov 15 '22 04:11

Frank