public function updateSpecializations(Request $request) { if(AjaxRequest::ajax()) { $this->validate($request, [ 'values.*' => 'numeric|min:1' ]); $values = Input::get('values'); if(null !== $values){ $values = array_slice($values,0,5); } $user = Auth::user(); if(count($values) == 0) { $user->specializations()->whereNotIn('sub_category_id', [0])->delete(); } else { foreach ($values as $key => &$value){ echo '['.$key."!".$value.']'; if($key < 5){ $doubledSpecializations = Specialization::where('sub_category_id',$value)->where('user_id',$user->id)->count(); if($doubledSpecializations != 0) { //$user->specializations()->whereIn('sub_category_id', $values)->delete(); } else { //TODO: ΠŸΠΎΡ‡Π΅ΠΌΡƒ ΠΎΠ½ ΠΌΠΎΠΆΠ΅Ρ‚ Π½Π° ΠΊΠ°ΠΆΠ΄ΠΎΠΌ этапС Ρ†ΠΈΠΊΠ»Π° ΠΏΠΎΠ»ΠΎΠΆΠΈΡ‚ΡŒ Π² Π±Π°Π·Ρƒ? $subCategory = SubCategory::findOrFail($value); $specialization = new Specialization; echo "->".$value."|".$key.":"; $subCategory->specializations()->save($specialization); $user->specializations()->save($specialization); $specialization->save(); } } } $user->specializations()->whereNotIn('sub_category_id', $values)->delete(); } return response()->json(['message' => 'saved!', 'values' => $values]); } } 

pic

    1 answer 1

    Ajax sent 2 requests at once, added a timeout of 500 milliseconds to it so that it would not spam, otherwise the base did not have time to react.

    The cycle itself was performed properly and the model was not saved;