<?php

namespace App\Imports;

use App\Models\Products;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\ToCollection;
use Throwable;

class DeleteProduct implements ToCollection
{
    use Importable;

    /**
     * @param Collection $collection
     */


    public function onError(Throwable $e)
    {
        // TODO: Implement onError() method.
    }

    public function collection(Collection $collection)
    {
        // TODO: Implement collection() method.
        $index = 0;
        foreach ($collection as $eachRow) {
            if ($index > 0) {
                $getProduct = Products::query()->where('product_code', $eachRow[0])->first();
                if (!is_null($getProduct)) {
                    $getProduct->delete();
                }
            }
            $index++;
        }
    }
}
