Laravel queues not saving entity to DB

laravel run queue in background
laravel multiple queues
laravel queue example
laravel check if queue is running
laravel async queue
laravel dispatch($job from command)
laravel dispatch($job with delay)
laravel job middleware

please help, I might be doing or understood something wrong. I am creating a mass upload to database, which takes a little bit of time so I decided to make async all and proccess it in background using Laravel Queues.

In controller I create an associated list, which is passed to Laravel Job.

foreach($data as $row)
    {
        $instantArray = array();
        $orderDate = \PHPExcel_Shared_Date::ExcelToPHPObject($row[3]);
        $issueDate = \PHPExcel_Shared_Date::ExcelToPHPObject($row[4]);
        $orderRow = array(
            'invoice_no' => trim($row[0]),
            'agent_id' => $row[1],
            'issued_at' => $orderDate->format('Y-m-d'),
            'received_at' => $issueDate->format('Y-m-d'),
        );
        $gameArray = array();
        $invoiceOrdersArray[] = $orderRow;
    }

$job = (new Import($invoiceOrdersArray));
dispatch($job);

In Laravel Job I am trying to create an entyty and upload it to database

class Import implements ShouldQueue
{
  use InteractsWithQueue, SerializesModels, Queueable, Dispatchable;
  protected $importData;
  public function __construct($importData)
  {
      $this->importData = $importData;
  }

  public function handle()
  {
      foreach($this->importData as $insert)
      {
         InvoicesOrder::create($insert);
      }
}

However, every time I am trying I can see in my logs:

 #0 app\Utilities\Updater.php(18): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'Trying to get p...', '...', 18, Array)
 #1 vendor\laravel\framework\src\Illuminate\Events\Dispatcher.php(348): Modules\Accounting\Entities\InvoicesOrder::App\Utilities\{closure}(Object(Modules\Accounting\Entities\InvoicesOrder))
 #2 vendor\laravel\framework\src\Illuminate\Events\Dispatcher.php(199): Illuminate\Events\Dispatcher->Illuminate\Events\{closure}('eloquent.creati...', Array)
 #3 vendor\laravel\framework\src\Illuminate\Events\Dispatcher.php(159): Illuminate\Events\Dispatcher->dispatch('eloquent.creati...', Array, true)
 #4 vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasEvents.php(148): Illuminate\Events\Dispatcher->until('eloquent.creati...', Object(Modules\Accounting\Entities\InvoicesOrder))
 #5 vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(636): Illuminate\Database\Eloquent\Model->fireModelEvent('creating')
 #6 vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(522): Illuminate\Database\Eloquent\Model->performInsert(Object(Illuminate\Database\Eloquent\Builder))
 #7 Modules\Accounting\Jobs\ImportFiles.php(39): Illuminate\Database\Eloquent\Model->save()
 #8 [internal function]: Modules\Accounting\Jobs\ImportFiles->handle()
 #9 vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(29): call_user_func_array(Array, Array)
 #10 vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(87): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
 #11 vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(31): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
 #12 vendor\laravel\framework\src\Illuminate\Container\Container.php(539): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
 #13 vendor\laravel\framework\src\Illuminate\Bus\Dispatcher.php(94): Illuminate\Container\Container->call(Array)
 #14 vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(114): Illuminate\Bus\Dispatcher->Illuminate\Bus\{closure}(Object(Modules\Accounting\Jobs\ImportFiles))
 #15 vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(102): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Modules\Accounting\Jobs\ImportFiles))
 #16 vendor\laravel\framework\src\Illuminate\Bus\Dispatcher.php(98): Illuminate\Pipeline\Pipeline->then(Object(Closure))
 #17 vendor\laravel\framework\src\Illuminate\Queue\CallQueuedHandler.php(42): Illuminate\Bus\Dispatcher->dispatchNow(Object(Modules\Accounting\Jobs\ImportFiles), false)
 #18 vendor\laravel\framework\src\Illuminate\Queue\Jobs\Job.php(69): Illuminate\Queue\CallQueuedHandler->call(Object(Illuminate\Queue\Jobs\RedisJob), Array)
 #19 vendor\laravel\framework\src\Illuminate\Queue\Worker.php(317): Illuminate\Queue\Jobs\Job->fire()

Whenever I do it in controller it works fine, it just loads very long. Maybe it is not possible to save entities in Job using queues?

Thank you.

In your job you insert the data in the $this->importData property.

but in the handle method, you're looping over $invoiceOrdersArray, where does that come from?

Should your loop, not look like this?

foreach($this->importData as $insert)
{
     InvoicesOrder::create($insert);
}

You don't even need the unused $order variable in the handle method.

Also, I think you need to use the InvoicesOrder class in the job. So the whole thing might look like this.

use namespace/to/InvoicesOrder;

class Import implements ShouldQueue
{
    use InteractsWithQueue, SerializesModels, Queueable, Dispatchable;

    protected $importData;
    public function __construct($importData)
    {
         $this->importData = $importData;
    }

    public function handle()
    {
        foreach($$this->importData as $insert)
        {
            InvoicesOrder::create($insert);
        }
    }
}

Laravel queue to store data, I wanna try to store data into database via database queue Laravel. Instead of pass Order object pass in job order data, And the in job save order. $order) { if (!$order->craete($this->data)) { // when not saved try again  Laravel queues provide a unified API across a variety of different queue backends, such as Beanstalk, Amazon SQS, Redis, or even a relational database. Queues allow you to defer the processing of a time consuming task, such as sending an email, until a later time.

Since queue workers are long-lived processes, they will not pick up changes to your code without being restarted. So, the simplest way to deploy an application using queue workers is to restart the workers during your deployment process. You may gracefully restart all of the workers by issuing the queue:restart command:

php artisan queue:restart

Model Not Saving to DynamoDB When Called From Job in Queue , Model Not Saving to DynamoDB When Called From Job in Queue #165 It instead saves the queued item to the MySQL DB instead. If I move the job method for saving the model object directly into the controller, I have no problem saving it to DynamoDB. <?php namespace App\Jobs; use App\Hit; use  First of all, database is used as the queue driver in the current project. Because it is simple, efficient and does not need to expand other third-party applications, MySQL database has been used as the queue driver. The online queue environment runs Ubuntu 16.04 + mysql5.7 + laravel5.6.

If anyone bumps into the similar problem, check in your entity if you are using any kind of presenters (I was using presentable trait), which adds fields like created/updated by. And what do we get from Auth::user()->id... Trying to get a property of non object, as queue is not an user.

Laravel 5. PDOException. QUEUE_DRIVER=database 1213 , I use QUEUE_DRIVER=database When I run more than 10 workers, I get the \​Database\{closure}(Object(Illuminate\Database\MySqlConnection), 'update `jobs` s. Array) #6 /home/www/vendor/laravel/framework/src/Illuminate/Queue/ The second worker should not pick the job as it is being reserved. Database Setup for Laravel Queues. We will use the database driver to process our queues, but you can use Amazon SQS or Redis if you have that setup already. I will stick to database. Before using the Laravel Queues, we need to make a jobs table in the database to store all queues. Laravel provides the table creation command out of the box, so

Queue Workers: How they work, You save server resources by avoiding booting up the whole app on while (​true) { // This process simply calls 'php artisan queue:work In case the queue.​failed configuration value is set, the database Application is not in maintenance mode; Worker is not paused Where did we get this job object? There is one entry for the job. So that means, the process of that job is not started. So Laravel, there is a concept called Queue Worker. We need to run that Queue Worker. Switch to the terminal and type the following command to start the queue worker. php artisan queue:work. In the terminal, it will say like this.

Notifications not saved to database, Setup a notification to send an email, save to the database and broadcast. Sending the <?php namespace App\Notifications; use App\Models\Order; use ->call(Object(Illuminate\\Queue\\Jobs\\DatabaseJob), Array) #15  Resetting The Database After Each Test. It is often useful to reset your database after each test so that data from a previous test does not interfere with subsequent tests. The RefreshDatabase trait takes the most optimal approach to migrating your test database depending on if you are using an in-memory database or a traditional database. Use

Laravel: Up and Running: A Framework for Building Modern PHP Apps, Laravel makes it easy to serve your queues using Redis, beanstalkd, Amazon's SQS (Simple Queue Service), or a database table. Sometimes you may not just want to save your users time, but you might have a process like a cron job This job could be shaped any way; it could just be a string, or an array, or an object. Configuration. The database configuration for your application is located at config/database.php.In this file you may define all of your database connections, as well as specify which connection should be used by default.

Comments
  • Yes, corrected my question, there is a little bit more going on in the Job, but I just deleted everything what is not necessary for the question.
  • The stack trace says it is trying to locate your InvoiceOrder in the Modules\Accounting\Entities\InvoicesOrder namespace, which is the same as the job. Is that definitely correct?