Issue
This Content is from Stack Overflow. Question asked by go_live
There is a system developed with Laravel, and I would like to store multiple selected data in the conditions column using the implode function.
Currently, it is saved as shown in the image below.
I want to save data in a form like “1,2,3,4” using the implode function.
How can I achieve what I want?
**Code
ShopController.php
public function store(Request $request)
{
$param = $request->except([
'logo',
'image1',
'image2',
'image3',
'image4',
'image5',
'logo_cache',
'image_cache1',
'image_cache2',
'image_cache3',
'image_cache4',
'image_cache5',
]);
if ($request->filled('logo_cache')) {
$param['logo'] = $request['logo_cache'];
} else {
if (!$validator->errors()->has('logo') && $request->has('logo')) {
$path = $request['logo']->store('public/'.config('constants.UPLOAD_IMG_SHOP'));
$filename = basename($path);
$param['logo'] = $filename;
$request['logo_cache'] = $filename;
} else {
$param['logo'] = null;
}
}
for ($i = 1; $i <= config('constants.SHOP_IMAGES_MAX'); $i++) {
if ($request->filled('image_cache'.$i)) {
$param['image'.$i] = $request['image_cache'.$i];
} else {
if (!$validator->errors()->has('image'.$i) && $request->has('image'.$i)) {
$path = $request['image'.$i]->store('public/'.config('constants.UPLOAD_IMG_SHOP'));
$filename = basename($path);
$param['image'.$i] = $filename;
$request['image_cache'.$i] = $filename;
} else {
$param['image'.$i] = null;
}
}
}
//Add code
if ($request->filled('conditions')) {
$param['conditions'] = implode(",", $request['conditions']);
}
if ($validator->fails()) {
return redirect('/shop/create')
->withErrors($validator)
->withInput();
}
$param['area_id'] = Auth::user()->area_id;
Shop::create($param);
session()->flash('alert_message', config('constants.ALERT.SUCCESS_CREATE'));
session()->flash('alert_style', 'success');
return redirect()->route('admin.shop.index');
}
edit.blade.php
<tr>
<th>Conditions</th>
<td>
<div class="c-form__check c-form__check_inline">
@foreach(config('constants.FORM.SHOP_CONDITIONS') as $key => $value)
<label><input type="checkbox" name="conditions[]" value="{{ $key }}" {{ old('conditions', $shop->conditions) == $key ? 'checked' : '' }}> {{ $value }}</label>
@endforeach
</div>
</td>
</tr>
Solution
This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.
This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.