I'm trying to insert some records into a dabase using Laravel's Eloquent ORM. The data is stored in an array which looks like this (there are more than one elements in the array)
The initial array which I'm looping through looks like this
array:1 [▼
"Product" => array:4416 [▼
0 => array:9 [ …9]
1 => array:9 [ …9]
2 => array:9 [ …9]
3 => array:10 [ …10]
---------------------------------------------------------------------
array:9 [▼
"ProductCode" => "EWS1025CAM"
"Vendor" => "ENGENIUS"
"ProductType" => "Security - Surveillance Indoor Camera"
"ProductCategory" => "Other"
"ProductDescription" => "Managed AP-CAM Indoor Dual Band 11ac 2T2R 300 867Mbps 2MP dome 4mm IR20m PoE.af μSDHC (Access point - Camera, Power Adapter (12V/1.5A), T-rail mounting kit, mou ▶"
"Image" => "https://www.it4profit.com/catalogimg/wic/1/EWS1025CAM"
"ProductCard" => "https://content.it4profit.com/itshop/itemcard_cs.jsp?ITEM=171208071213009310&THEME=asbis&LANG=ro"
"AttrList" => array:1 [▶]
"Images" => array:1 [▶]
]
I'm looping through it and inserting values like this
foreach ($phpDataArray['Product'] as $key => $value)
{
$insert = new FurnizorProduse();
$insert->product_id = $value['ProductCode'];
$insert->product_data = json_encode($value);
$insert->date = new \DateTime();
$insert->module_id = 58;
$insert->save();
}
Even though the data is inserted correctly into the db, I get "Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given".
The line of code causing this is $insert->product_id = $value['ProductCode'];
as if I replace the $value['ProductCode'];
wiht an integer let's say, no more errors.
Any ideas ? Thanks !
CodePudding user response:
In one of your iteration of $phpDataArray['Product'], you don't have $value['ProductCode'], is my guess.
Try to do: if(is_array($value) && isset($value['ProductCode'])) { $insert = new FurnizorProduse(); ... }