Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

gokyo66's avatar

Can't related three table while inserting data

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

0 likes
2 replies
Wraith's avatar

first yoy saved product and using this product model to save conditiontest like $product->conditiontest()->saveMany right? but before u have if(isset($value['value'])) and after this if u have $condition->foodsimulant()->saveMany but what if if(isset($value['value'])) return false ? u still trying save $condition->foodsimulant()->saveMany without any $condition product like $product->conditiontest->foodsimulant()->saveMany

What is your $condition ? where u create model ?

gokyo66's avatar

"first yoy saved product and using this product model to save conditiontest like $product->conditiontest()->saveMany right?" Correct

"but before u have if(isset($value['value'])) and after this if u have $condition->foodsimulant()->saveMany but what if if(isset($value['value'])) return false ?" Means that there is no value (true/false) so if NULL I don't need to save it

"u still trying save $condition->foodsimulant()->saveMany without any $condition product like $product->conditiontest->foodsimulant()->saveMany" You're right, I wasn't aware that you could do that. I tried $product->conditiontest()->foodsimulant()->saveMany but didn't work (note the parenthesis after conditiontest. Now I try without them and maybe this is the solution. In any case this is a just a first draw and are missing all the control as you correct noticed

"What is your $condition ? where u create model ?" Don't know exactely what you mean with "what is your $condition. The model is under DMSystem following the code

namespace DMSystem;

use Illuminate\Database\Eloquent\Model;
use DMSystem\Product;
use DMSystem\Foodsimulant;

class Conditiontest extends Model
{

    protected $fillable = ['product_id',
                           'substance',
                           'sml',
                           'fcm',
                           'ref',
                           'cas',
                           'migration_type'];

    public function product()
    {
       return $this->belgongsTo('DMSystem\Product');
    }

    public function foodsimulant()
    {
       return $this->hasMany('DMSystem\Foodsimulant');
    }

}

Please or to participate in this conversation.