I know the question has been asked many times on this forum before, but I feel mine is a bit different. Please also keep in mind, that I am still in the learning stage of Laravel.
I get an XML feed from a supplier and then have to parse the data (to my knowledge). I can get the products in an array and I cat even display it in my view, but I want to import it into an MySQL DB. So this is what I am doing in my controller:
$xml = XmlParser::load(env('ESQ_XML'));
$data = $xml->parse([
'products' => ['uses' => 'product[code,cat,description,stock,price,img]'],
]);
$products = $data['products'];
foreach ($products as $product){
$product = new Product();
$productCode = $product['code'];
$product->code = $productCode;
$product->category = $product['cat'];
$product->description = $product['description'];
$product->price = $product['stock'];
$product->stock = $product['price'];
$product->image = $product['img'];
$product->save();
};
if I dd($product) in the foreach, I get:
array:6 [▼
"code" => "86UK7050.PVA"
"cat" => "Televisions- Smart Ultra HD"
"description" => "LG 86UK7050 Series 86" Super UHD IPS 4K Display Smart TV - 3840 x 2160 Resolution, Super UHD Mastering Engine, 200Hz Frame Rate, Built-In Analogue Broadcasting ▶"
"stock" => "Yes"
"price" => "R 43 477.39"
"img" => "https://www.xyz.co.za/ProdImg/75UK7050PVA-001.jpg"
]
and if I dd($product['code']), I get:
"86UK7050.PVA"
So I am not sure where I am going wrong?
Full error:
Illuminate \ Database \ QueryException (23000)
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'code' cannot be null (SQL: insert into `products` (`code`, `category`, `description`, `price`, `stock`, `image`, `updated_at`, `created_at`) values (, , , , , , 2019-02-18 06:18:16, 2019-02-18 06:18:16))
Fillable fields in my model:
protected $fillable = [
'code',
'category',
'description',
'price',
'stock',
'image'
];