2017-01-30 2 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

당신이 정말로하고 싶은 것은 비동기식으로 프로세스를 처리하는 것과 같고 작업 클래스를 작성할 수 있다고 생각합니다. 작업 클래스는 컨트롤러 함수에서 디스패치 될 수 있으며 백그라운드에서 실행됩니다.

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); 
} 

을 그리고 당신을 위해 그것을해야

동일한 기능의 예 작업이 같을 것이다 .

관련 문제