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

Sinnbeck's avatar

@oxbir any chance you could just put this on github? Think it will be earlier to make a pr with the code. Then you can see how it's supposed to be

shahr's avatar
Level 10

I think this is what you said

    foreach ($request->study as $key => $value) {
        Educational::updateOrCreate(['user_id' => auth()->id()], [
            'grade_id' => $request->input('grade_id'),
            'field' => $request->input('field'),
            'institution_id' => $request->input('institution_id'),
            'branch' => $request->input('branch'),
            'institution_education' => $request->input('institution_education'),
            'gpa' => $request->input('gpa'),
            'nation_id' => $request->input('nation_id'),
            'province_id' => $request->input('province_id'),
            'town_id' => $request->input('town_id'),
            'province_name' => $request->input('province_name'),
            'town_name' => $request->input('town_name'),
            'entrance' => $request->input('entrance'),
            'graduate' => $request->input('graduate'),
            //'currently_studying' => $request->has("currently_studying.$key"),
        ]);
    }
Snapey's avatar

oh my god .....

have you been listening to ANYTHING?

shahr's avatar
Level 10

I replace it

    foreach ($request->study as $key => $value) {
        Educational::updateOrCreate(['user_id' => auth()->id()], [
            'grade_id' => $request->input('grade_id'),
            'field' => $request->input('field'),
            'institution_id' => $request->input('institution_id'),
            'branch' => $request->input('branch'),
            'institution_education' => $request->input('institution_education'),
            'gpa' => $request->input('gpa'),
            'nation_id' => $request->input('nation_id'),
            'province_id' => $request->input('province_id'),
            'town_id' => $request->input('town_id'),
            'province_name' => $request->input('province_name'),
            'town_name' => $request->input('town_name'),
            'entrance' => $request->input('entrance'),
            'graduate' => $request->input('graduate'),
            //'currently_studying' => $request->has("currently_studying.$key"),
        ]);
    }

This code also empties the data in my table that was filled.

Snapey's avatar

because you are using fields from $request and not from $value

MichalOravec's avatar

This is really ridiculous.

foreach ($request->study as $key => $study) {
    Educational::updateOrCreate([
        'user_id' => auth()->id(),
        'grade_id' => $study['grade_id'],
        'institution_id' => $study['institution_id'],
    ], [
        'field' => $study['field'],
        'branch' => $study['branch'],
        'institution_education' => $study['institution_education'],
        'gpa' => $study['gpa'],
        'nation_id' => $study['nation_id'],
        'province_id' => $study['province_id'],
        'town_id' => $study['town_id'],
        'province_name' => $study['province_name'],
        'town_name' => $study['town_name'],
        'entrance' => $study['entrance')]
        'graduate' => $study['graduate'],
        'currently_studying' => $request->has("study.{$key}.currently_studying")
    ]);
}

First array of updateOrCreate is what has to be unique. I guess they are user_id, grade_id and institution_id.

1 like
automica's avatar

@oxbir what are you hoping using that will do?

Because you are currently only ever able to create one record for a user even though you are trying to save many when you add multiple rows in your form.

You need only one group of arguments, not two.

like I said on the (5) which you said you read earlier, and also said you understood. But I don’t think you did.

Snapey's avatar

I also suggested (can't remember in which thread) to check your data and get familiar with the format. How did you jump to trying to write the data to the database?

MichalOravec's avatar

@automica Yeah, because this is too much...

Or just

foreach ($request->study as $key => $study) {
    $data = collect($study);

    Educational::updateOrCreate($data->only(['grade_id', 'institution_id']) + [
        'user_id' => auth()->id()
    ], $data->except(['grade_id', 'institution_id', 'currently_studying']) + [
        'currently_studying' => $request->has("study.{$key}.currently_studying")
    ]);
}
2 likes
MichalOravec's avatar

But if I could choose naming of inputs it would be

<select name="educationals[][grade_id]">
    //
</select>

<input type="text" name="educationals[][branch]">

// and so on

Then code on the server side looks like

foreach ($request->educationals as $key => $educational) {
    $data = collect($educational);

    Educational::updateOrCreate($data->only(['grade_id', 'institution_id']) + [
        'user_id' => auth()->id()
    ], $data->except(['grade_id', 'institution_id', 'currently_studying']) + [
        'currently_studying' => $request->has("study.{$key}.currently_studying")
    ]);
}
1 like
Snapey's avatar

@michaloravec

I suggested "study[key]grade_id">

so that there are multiple study entries, each of which is an associative array of fields. There is no need to put braces around the field, only additional braces needed if the input allows multiples

1 like
MichalOravec's avatar

@snapey No, in the code what I posted a few minute ago it has to be part of array. So it's neccessary be inside [field] like

name="study[key][grade_id]">
1 like
MichalOravec's avatar
Level 75

@oxbir Is it really problem to convert it to an array?!

foreach ($request->educationals as $key => $educational) {
    $data = collect($educational);

    Educational::updateOrCreate($data->only(['grade_id', 'institution_id'])->toArray() + [
        'user_id' => auth()->id()
    ], $data->except(['grade_id', 'institution_id', 'currently_studying'])->toArray() + [
        'currently_studying' => $request->has("study.{$key}.currently_studying")
    ]);
}
1 like
shahr's avatar
Level 10

@michaloravec

Is it correct in blade.php

<input id="field" name="educationals[key][field]" class="form-control">
Previous

Please or to participate in this conversation.