<?php

namespace App;

use App\Product;
use App\Category;
use App\ProductCategory;
use Illuminate\Database\Eloquent\Model;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;

class ProductsImport implements ToModel, WithHeadingRow
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */

    public function model(array $row)
    {
        $productID = Product::insertGetId([
            'sku'     => $row['sku'],
            'type'    => $row['product_type']
        ]);


        ProductCategory::create([
            'product_id' => $productID,
            'category_id' => $this->getCategoryIDByName($row['categories'])
        ]);

        ProductAttribute::create([
            'product_id' => $productID,
            'attribute_id' => $this->getAttributeIDByName($name)

        ]);



    }


    private function getCategoryIDByName($name){
        return Category::where('name',$name)->first()->id;
    }
}