2017-01-30 14 views
0

私はLaravel 5.4キューを使用しています。私はExcelを読んで、数秒後にそのレコードのDBエントリを作成したいと思います。Laravel 5.4のキューで関数を渡す方法は?

$queue = Queue::later(10,'LogMsg', app('App\Http\Controllers\getFileController')->myfunc($name)); 
return $queue; 

これは私の呼び出し関数です。まずこれを渡すことができますか?

public function myfunc($name) { 
    $f_data = Excel::load('public/invoices/'.$name, function($reader) { 
     })->get();  
    if(!empty($f_data) && $f_data->count()){ 
      foreach ($f_data as $key => $row){       
        $inv = new final_tables; 
        foreach ($row as $key1 => $col){ 
         $inv->$key1 = $row->$key1; 
        } 
        $inv->save(); 
      } 
     } 
    return 'done'; 
} 

答えて

0

あなたが本当にやりたいことは、Excelを非同期に処理することで、同じことをするジョブクラスを書くことができると思います。ジョブクラスはコントローラー関数からディスパッチすることができ、バックグラウンドで実行されます。

同じ機能の例ジョブは次のようになります。今、あなたはそのようなあなたのコントローラ機能から同じ仕事を派遣することができます

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

    protected $filePath; 

    /** 
    * Create a new job instance. 
    * 
    * @return void 
    */ 
    public function __construct(string $filePath) 
    { 
     $this->filePath = $filePath; 
    } 

    /** 
    * Execute the job. 
    * 
    * @return void 
    */ 
    public function handle() 
    { 
     $fileData = Excel::load($this->filePath, function($reader) { 
     })->get(); 

     //Whatever you want to do with the file here, for eg. create a DB entry 
     return 'done'; 
    } 
} 

use Carbon\Carbon; 


public function someControllerAction(Request $request) 
{ 
    $filePath = //Save file and obtain filepath 

    $job = (new ReadExcelAndSaveRecordsToDB($filePath)) 
       ->delay(Carbon::now()->addMinutes(10)); 

    dispatch($job); 
} 

そして、それはあなたのためにそれを行う必要があります。

関連する問題