Search code examples
queuelaravel-5.3

Make Laravel use database Queue instead of sync


I need help with Laravel Queues if someone can help me further, i have done these steps till now

  1. changed the QUEUE DRIVER in .env file to database

  2. created migrations for queue table jobs and failed-jobs

    php artisan queue:table
    php artisan queue:failed-table
    

and i have run php artisan migrate

  1. created a new Job

    php artisan make:job SendNewArticleNotification
    

Update:

Ok i have created a route /queue

Route::get('/queue', function () {

    dispatch(new \App\Jobs\SendNewArticleNotification);

});

and in my Job SendNewArticleNotification i have this

<?php

namespace App\Jobs;

use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class SendNewArticleNotification implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {

        $users = User::all();

        $article = \App\Article::first();

        foreach ($users as $user) {
            $user->notify(new \App\Notifications\Published($article));
        }    


    }
}

And when i hit my /queue route the emails are beeing sent but they are not using the database table....it takes more than 30 seconds to send 50 mails...

UPDATE

Ok i finally figured it out that dispatch() needs to be inside of a controller method because the controller has the trait dispatches jobs....and now my jobs are in queue in DB table jobs....so how do i trigger them behind the scenes?


Solution

  • Put your dispatch in your controller method and then run

    php artisan queue:work