How to dispatch a job to the default queue in Laravel?

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:

  1. Create a new job using the following artisan command:
php artisan make:job MyJob

This will create a new job file MyJob.php in the app/Jobs directory of your Laravel application.

  1. Open the MyJob.php file and define the logic that needs to be performed in the handle() method. For example:
public function handle() { // Logic here }
  1. Now, you can dispatch the job to the default queue using the 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.

  1. By default, Laravel uses the 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.