I have a controller that creates a new model then passes it to the view
public function fill_site_form($id, $step_id, $form_id){
$form = new FormEntry();
$form->site_id = $id;
$form->form_id = $form_id;
$form->step_id = $step_id;
$form->entry_json = Form::find($form_id)->form_json;
$form->save();
return view('sites.fill_site_form', ['form' => $form]);
}
I need it to only create one record in the db but it creates 2 records everytime I go to that route.
I have removed the ->save and then no records are inserted into the DB.
Any suggestions?
Edit:
Image of DB entries on the $form->save: SCREENSHOT IMAGE LINK
-
The DB Schema:
Schema::create('form_entries', function (Blueprint $table) { $table->increments('id'); $table->integer('site_id'); $table->integer('form_id'); $table->integer('step_id'); $table->text('entry_json', 600000); $table->timestamps(); });
-
The code that receives the ajax from the sites.fill_site_form view
public function update_site_ajax($id, Request $request){ $entry = FormEntry::find($id); $entry->entry_json = json_encode($request->form_json); $entry->save(); return $request->all(); }
-
Front end AJAX code:
$('#submit_button').click((e)=>{ $.ajax({ type:'PATCH', url:'/site/' + document.getElementById('form_id').value, data: {'form_json' : renderer.userData}, success:function(data){ $.notify("Form successfully Updated!", { position:"top center", className: 'success' } ); console.log('Response: ', data) } }); });