,

Dispatching jobs with Laravel Tinker

Laravel Tinker is the poweful REPL for the Laravel Framework. It allows you to interact with your Laravel application through command line interface. Tinker is preinstalled in every Laravel project. If you have previously removed it, you can install it using the composer.

composer require laravel/tinker

To enter the tinker session, use following command with the artisan.

php artisan tinker

There are few ways to dispatch the Jobs in Laravel framework. You can dispatch the jobs in sync or onto the queue. Dispatching the jobs is fairly simple. Start the tinker session and dispatch the job as given.

$arg1 = 1;
$arg2 = 2;
MyCustomJob::dispatchSync($arg1,$arg2);

In similar way you can use the dispatch() method to dispatch the job on the queue.

$arg1 = 1;
$arg2 = 2;
MyCustomJob::dispatch($arg1,$arg2);

When you want to dispatch multiple jobs at a time you can use either Batch or Chain. Batch lets you dispatch the multiple jobs on the queue which runs in parallel fashion. If you want to force the jobs to execute in sequential manner you need to use the Chain. Below are the examples of the code respectively.

//Dispatching in a batch
Bus::batch([new MyCustomJob($args),new MyCustomJob2()])->dispatch();

//Dispatching in a chain
Bus::chain([new MyCustomJob($args),new MyCustomJob2($args)])->dispatch();

Above snippets will let you dispatch the jobs on your default queue. You may need to start the queue worker to process the jobs if, the worker aren’t running already. Start the queue worker with below command.

php artisan queue:work

In this way you can dispatch the jobs with Laravel Tinker.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *