Reputation: 718
In a Laravel blade template, we have a form with some optional radio buttons and check boxes, like
<input type="checkbox" id="A" name="A">A <br />
<input type="checkbox" id="B" name="B">B <br />
and radio buttons
<input type="radio" id="R1" name="AINE" value="1"> Yes
<input type="radio" id="R2" name="AINE" value="0"> No
In controller, in post method, we have
$input = Input::all();
DB::connection('datab1')->table('tab1')->insert(array(
'A' => array_key_exists('A', $input) ? $input['A'] : null,
'B' => array_key_exists('B', $input) ? $input['B'] : null,
'AINE' => array_key_exists('AINE', $input) ? $input['AINE'] : null,
));
But on submitting the form, it always insert 1 in all the three columns in database, even if NONE of the radio button is selected, or checkbox is checked. Can somebody please explain?
Upvotes: 0
Views: 1335
Reputation: 73
You have to set your check boxes or radio buttons in array mode forexample:
<input type="checkbox" id="A" name="A[]">A <br />
and foreach your array in your controller like this:
foreach(Input::get('A') as $name)
{
$model=new yourModelName;
$model->yourRowName=$name;
$model->save();
return->redirect()->back();
}
And its finished.
Upvotes: 1
Reputation: 9444
It should be something like the following:
public function post(Request $request)
{
DB::connection('datab1')
->table('tab1')
->insert([
'A' => $request->has('A') ? $request->A : null,
'B' => $request->has('B') ? $request->B : null,
'AINE' => $request->has('AINE') ? $request->AINE : null,
]
);
}
Upvotes: 0