2017-01-30 4 views
0

Ich benutze Laravel 5.4 Warteschlange. Ich würde gerne Excel lesen und DB Einträge dieser Datensätze nach ein paar Sekunden machen.Wie übergibt man die Funktion in der Warteschlange in Laravel 5.4?

Das ist meine Anruffunktion, erstens kann ich so weitergehen?

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

Antwort

0

Ich denke, was Sie wirklich wollen, eine Excel-asynchron zu tun ist, Prozess und Sie können eine Job-Klasse schreiben, das gleiche zu tun. Die Jobklasse kann von der Controller-Funktion ausgelöst werden und wird im Hintergrund ausgeführt.

wäre ein Beispiel Job die gleiche Funktionalität wie folgt aussehen: Sie

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

Und das sollte es tun:

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

Jetzt können Sie die gleiche Arbeit von Ihrem Controller-Funktion wie so versenden .

Verwandte Themen