I am trying to store data in database by using laravel queue. I want to show user a quick message that "your file is being imported" and want to store data using queue. I have used database as QUEUE_DRIVER in env file. When I try to store data it creates entry in jobs table but doesn't store anything in demo_tbl.
Here is what I have done so far -
Controller
public function import(Request $request)
{
Excel::filter('chunk')->load($request->file('import_file')->getRealPath())->chunk(250, function($reader)
{
$this->dispatch(new ImportDistributor($reader->toArray()));
});
dd('your file is being imported. we will inform you once it is done.');
}
Job -
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use App\Model\DemoTableModel;
use DB;
class ImportDistributor implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* @return void
*/
protected $data;
public function __construct(array $request)
{
$this->data = $request;
}
public function handle()
{
if(isset($this->data) && !empty($this->data))
{
foreach($this->data as $val)
{
DemoTableModel::insert($val);
}
}
}
}
Model
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class DemoTableModel extends Model
{
protected $table = 'demo_tbl';
protected $fillable = [
'name',
'city'
];
}
Any help would be appreciated.
Thanks.