If storing in one field, look up the implode function in the PHP manual.
Jan 28, 2021
6
Level 5
Array to string conversion
I am struggling with inserting multiple checkbox values. Below is my form for the checkboxes and my submit controller function. I know this isn't right but I really don't know what to do. Can someone please point me in the right direction?
Form
<div class="row">
<div class="col-md-7 pl-0">
<label for="email" class="font-weight-bold m-t-10 m-r-10">Type of employment desired:</label>
<div class="form-check-inline">
<label class="form-check-label">
<input type="checkbox" class="form-check-input" name="emptype[]" value="fulltime">Full Time
</label>
</div>
<div class="form-check-inline">
<label class="form-check-label">
<input type="checkbox" class="form-check-input" name="emptype[]" value="parttime">Part Time
</label>
</div>
<div class="form-check-inline">
<label class="form-check-label">
<input type="checkbox" class="form-check-input" name="emptype[]" value="seasonal">Seasonal
</label>
</div>
</div>
</div>
controller
ublic function submit(Request $request)
{
$app = Application::create($request->all() + [
'user_id' => Auth::user()->id,
$app['emptype'] = $request->input('emptype')
]);
return Redirect::to(URL::previous())->with('success', 'Your employment application has been submitted!');
}
Level 5
I figured it out. In my Model I put
protected $casts = [
'emptype' => 'array',
];
and in the controller
public function submit(Request $request)
{
$request->validate([
'dateavailable' => 'required|date',
'citizen' => 'required',
'workpapers'=> 'required',
'emptype' => 'required',
]);
$emp = array($request->emptype);
$app = Application::create($request->all() + [
'user_id' => Auth::user()->id,
'emptype' => json_encode($emp),
]);
return Redirect::to(URL::previous())->with('success', 'Your employment application has been submitted!');
}
Please or to participate in this conversation.