php - Why is the data being saved in the wrong order in this JSON? - Laravel

As you can see I wanna save the same data fields x2 at the same time in DB.

But, because of its structure, in the first <div> there's not language and his options.

In the EnviarCurriculumController.php it's suppose if there's not info there it's gonna be substitute by '', ergo (NULL) in DB.

But instead of that, the data of language_option1 and language_option2 are saving in the first row of DB, the one corresponding to the first <div>.

language is saving in his correct row in DB, but language_option1 and language_option2 in the previous one.

What should I do?

Data saved in DB

nif name language language_option1 language_option2
1 Ben (NULL) Intermediate Rookie
2 John English (NULL) (NULL)

Answer

Solution:

Your problem is that you do not have a good {-code-1} to solve this.

You have these fields in this exact order:

  1. Input text nif[]
  2. Input text name[]
  3. Input text nif[]
  4. Input text name[]
  5. Input text language[]
  6. Input text language_option1[]
  7. Input text language_option2[]

So, when you send all this info, you will have (for example) this data:

{
    "nif": [
        "first nif",
        "second nif"
    ],
    "name": [
        "first name",
        "second name"
    ],
    "language": [
        "first language"
    ],
    "language_option1": [
        "first language_option1"
    ],
    "language_option2": [
        "first language_option2"
    ]
}

So, you have 2 {-code-1{-code-12}} and 2 {-code-11} (index {-code-12} and index 1) but you have only index {-code-12} for language, language_option1 and language_option2. So your problem is your {-code-1}, not your PHP code (that is correct).

Source