To dispatch a job to the default queue in Laravel, you can use the dispatch()
function provided by Laravel. Here are the steps to do it:
php artisan make:job MyJob
This will create a new job file MyJob.php
in the app/Jobs
directory of your Laravel application.
MyJob.php
file and define the logic that needs to be performed in the handle()
method. For example:public function handle()
{
// Logic here
}
dispatch()
function. You can do this from any part of your Laravel application. For example, if you want to dispatch the job from a controller, you can do:use App\Jobs\MyJob;
public function someAction()
{
dispatch(new MyJob());
}
This will push the job to the default queue.
sync
queue driver, which runs the job synchronously. If you want to use a different queue driver, you can specify it in the QUEUE_CONNECTION
environment variable or by setting it in the config/queue.php
file.That's it! Now, when the queue worker runs, it will process the job from the default queue.