jeudi 3 mai 2018

How to pass the current class instance to a Job in Laravel?

I have a scenario where I want to pass the current class instance into a Job for asynchronous execution in Laravel, so that I can use the existing instance's resources.

When I pass "$this" as a parameter(as shown in the code below) to the dispatch() method, I get the below exception:

Exception: Serialization of 'Closure' is not allowed in /home/vagrant/code/biz/vendor/laravel/framework/src/Illuminate/Queue/Queue.php:92

How can this be performed in Laravel 5.1?


TestDataTable.php

class TestDataTable extends Datatable {

  public function excel()
  {
     $excel = app('excel');
     dispatch(new MyExcelJob($excel,$this));
  }

}


MyExcelJob.php

class MyExcelJob extends Job implements SelfHandling, ShouldQueue
{
   public function __construct($excel, $instance)
   {
    $this->excel = $excel;
    $this->instance= $instance;
   }

  public function handle()
  {
    //handler
  }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire