Untitled

mail@pastecode.io avatar
unknown
php
22 days ago
929 B
4
Indexable
Never


Excel::import(new VehiclesImport, $path);

====== 

class VehiclesImport implements ToCollection, WithHeadingRow, WithChunkReading, ShouldQueue
{

    public function collection(Collection $rows)
    {
        $rows->chunk(1)->each(function ($chunk) {
            ProcessVehicleRow::dispatch($chunk);
        });
    }

    public function chunkSize(): int
    {
        return 3;
    }
}

====== 

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

    protected $rows;

    public function __construct(Collection $rows)
    {
        $this->rows = $rows;
    }

    public function handle()
    {
        foreach ($this->rows as $row) {
            $this->processRow($row);
        }
    }

    protected function processRow($row)
    {

        $nv = new Vehicle();

        $nv->name = $row['title'];
        
        .....

        $nv->save();
    }
}
Leave a Comment