One form, one button two tables, populate one and delete the other
i have a form with 3 form fields and one button on the form, i also have two table in the database one is to populate the form with when i click on the button and the other table is to delete a row from it, do anyone now how i can use this one button on a form to fill one table and delete the data in the second table thanks in advance
Assuming you have a reference to both tables in your form then you should be able to do this easily in the controller method you post the form to.
Say you post the form to FormController@update. You can then do something like this:
public function update(Request $request, Model1 $model_1)
{
// extract all your form data
$input_1_value = $request->input('input_1');
$input_2_value = $request->input('input_2');
$input_3_value = $request->input('input_3');
// then add logic to firstly update the data for the first table
$model_1->update([
'field_1' => $input_1_value,
'field_2' => $input_2_value,
]);
// then look up the reference you want to delete from in the second table
$model_2 = Model2::find($input_3_value);
$model_2->delete();
return redirect()->back();
}
@segun you want to do something like this then (double check in the docs):
public function update(Request $request, Model1 $model_1)
{
// extract all your form data
$input_1_value = $request->input('input_1');
$input_2_value = $request->input('input_2');
$input_3_value = $request->input('input_3');
DB::transaction(function()
{
// update first table
$model_1->update([
'field_1' => $input_1_value,
'field_2' => $input_2_value,
]);
// delete from in the second table
$model_2 = Model2::find($input_3_value);
$model_2->delete();
});
}
depending on what you are doing you may also want to set up a Eloquent relationship between the two table