Hello Experts, I got the following situation I'm struggeling a little bit, to say at least.
The following image shows datbase table and how they are related https://i.imgur.com/166CgsL.png
I created the Model for each in the way:
Product -> Conditiontest (hasMany)
Conditiontest -> Product (belongsTo)
Conditiontest->Foodsimulant (hasMany)
Foodsimulant->Conditiontest->(belongsToMany)
A sample of data coming from the request are:
[2019-11-15 07:49:41] local.INFO: array (
'film_type' => 'PET',
'chosen_supplier' => 'Dummy Supplier',
'item_code' => 'KPET T01',
'micron' => '50',
'data_ethylene_glycol' =>
array (
'substance' => 'ethylene glycol',
'value' => true,
'sml' => '30',
'fcm' => '227',
'ref' => '16690',
'cas' => '107-21-1',
'migration_type' => 'Specific Migration',
),
'data_diethylene_glycol' =>
array (
'substance' => 'diethylene glycol',
'value' => true,
'sml' => '30',
'fcm' => '263',
'ref' => '13326',
'cas' => '111-46-6',
'migration_type' => 'Residual Content',
),
'form_data_ethylene_glycol' =>
array (
0 =>
array (
'test_type' => '30 min at 100°C',
'food_simulant' => '10% Ethanol',
'value_test' => '35',
),
1 =>
array (
'test_type' => '4 hours at 60°C',
'food_simulant' => '3% Acetic acid',
'value_test' => '54',
),
)
)
Now everything works fine to save, Product and Conditiontest but not for the foodsimulant because I can't relate the id of the Conditiontest table to the conditiontest_id fiedl of the Foodsimulant table.
Following an extract of the code where I save the various info in the database with a comment where it doesn't work
foreach ($data as $key => $value)
{
//Se i valori non sono di tipo array, salvo i dati
if(!is_array($value))
{
$product->itemcode = $data['item_code'];
$product->micron = $data['micron'];
$product->type = $data['film_type'];
$product->supplier = $data['chosen_supplier'];
//Salvo i dati relativi al prodotto
$product->save();
}
else
{
//Salvo i valori delle sostanze selezionate ed il tipo di migrazione fatt
if(isset($value['value']))
{
$product->conditiontest()->saveMany([new Conditiontest($value)]);
}
/*
This is the part that doesn't work, because I can't
figure it out how to get the id from the condition table, all the other values
get saved properly but not the condition_id
*/
foreach($value as $key01=>$value01){
if(array_key_exists ('0' , $value))
{
$condition->foodsimulant()->saveMany([new FoodSimulant($value01)]);
}
}
So, hoping that I've explain myself, if some of you can help me out, that would be very much appreciated