oxbir
1 week ago
176
8
Laravel

How to update and insert with firstOrNew?

Posted 1 week ago by oxbir

My form is like this demo.

form

When I click Add other academic record, it add new row, and when I'm filling new data And in the last step, when I click on the submit, nothing happens in the database.

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

Please sign in or create an account to participate in this conversation.