@oxbir great.
So number(5). What do you think you should do?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
@oxbir great.
So number(5). What do you think you should do?
@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
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"),
]);
}
oh my god .....
have you been listening to ANYTHING?
@sinnbeck good idea,
Can you help?
@oxbir find the line where I said to use ‘updateOrCreate’
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.
because you are using fields from $request and not from $value
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.
@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.
@michaloravec got too painful to watch?
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?
@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")
]);
}
@michaloravec a very elegant refactor sir.
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")
]);
}
And I'm done with this thread. Bye.
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
@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]">
@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")
]);
}
Is it correct in blade.php
<input id="field" name="educationals[key][field]" class="form-control">
How I said. I'm done.
@oxbir STOP marking me on the thread!!!
This is spamming!!!
Thank You For Answer...
But there is a problem and that is that it does not update.
Please or to participate in this conversation.