I would use interface in my Jobs and get contextual implementation of it in Jobs classes.
I read all tuts.
I register it:
class AppServiceProvider extends ServiceProvider
{
public function register()
{
$this->app
->when(SendinBlueJob::class)
->needs(MessageProviderInterface::class)
->give(SendinBlueService::class);
}
}
I dispatch job:
class MessageObserver
{
public function created(MessageInterface $message)
{
SendinBlueJob::dispatch($message);
}
}
In job class I want to get binded service:
class SendinBlueJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $message;
public $messageProvider;
public function __construct(MessageInterface $message, MessageProviderInterface $messageProvider)
{
$this->message = $message;
$this->messageProvider = $messageProvider;
$this->handle();
}
public function handle()
{
dd($this->messageProvider);
}
}
I can't dispatch job because server throws error:
Too few arguments to function App\Jobs\SendinBlueJob::__construct(), 1 passed in /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Bus/Dispatchable.php on line 16 and exactly 2 expected
I understand it, but I don't understand why Laravel doesn't injecting binded service if interface is called in constructor.