2

I'm trying to get queues working in laravel 5 and the queue listener is outputting:

php artisan queue:listen

[ErrorException]
Undefined index: table

The "jobs" and "failed_jobs" tables are present, config.php is set to "database".

A search of the laravel forum and google has not yielded a solution, amy ideas where to look?

z900collector
  • 1,014
  • 8
  • 10

2 Answers2

0

This is most likely not a fault of the Laravel Queue system. Instead, this error is thrown by PHP when an array is to be accessed by an unknown/unset element.

"Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP

For example:

    // Initialise an empty array
    $myArray = [];

    // Attempt to access an element that hasn't been set yet
    echo $myArray['breadCake'];

In your case, have a look through your code that deals with queueing and search for ['table'], ["table"] or anything that would need a "table" set.

It may be handy to anyone reading this and considering using Laravel Queues to remember a few things:

  • Queues are asynchronous and do not have access to the variables you once set in the application unless you pass them into something queue-able.

  • In Laravel 5, capture all the data you require for a job to exist in the Event __construct() method. Events have access to a trait called SerializesModels. You can pass your models as instances (with attributes) to the __construct() method (such as __construct(User $user). Assign your variables to the Event class scope (for example: $this->user = $user). This is passed to the EventListener handle(Event $event) method. This is called when the queue is being processed. The __construct() should be blank (or filled in with repositories / services / other handy bits and pieces).

  • You can access the objects passed to the handle(Event $event) method:

    public function handle(MyEvent $myEvent) 
    {
        $this->user = $myEvent->user;
        $mailData = ['user'=>$this->user];
        Mail::queue('viewdir.view',$mailData, function($message) use ($mailData) {
            $message->to($mailData['user']->email);
            // other stuff
        });
    }
    

I hope this helps anyone reading.

ImAtWar
  • 1,073
  • 3
  • 11
  • 23
Justin Origin Broadband
  • 1,492
  • 1
  • 11
  • 14
0

So I had the same problem but I discovered that I had set my driver to sync while using the database as the sync driver. setting my driver as database solved it for me

  • 1
    Hello and welcome to Stackoverflow. the OP already stated that he changed the driver to database in his question, your solution may be right but you should put more information such as changing it in .env not in config file so the OP don't get confused. have a good day ahead – George Jun 08 '20 at 19:53