我使用引导程序形式从数组中的复选框值获取输入。
我正在使用数组来存储复选框值。我如何将此数组转换为 string 。因为数据库只接受字符串值。
这是我的代码
<div class="form-group col-md-12">
<div class="custom-control custom-checkbox custom-control-inline">
<input type="checkbox" id="eduPrimary" name="education[]"
class="custom-control-input" value="primary" />
<label class="custom-control-label" for="eduPrimary">primary</label>
</div>
</div>
<div class="form-group col-md-12">
<div class="custom-control custom-checkbox custom-control-inline">
<input type="checkbox" id="eduSecondary" name="education[]"
class="custom-control-input" value="secondary" />
<label class="custom-control-label" for="eduSecondary">secondary</label>
</div>
</div>
<div class="form-group col-md-12">
<div class="custom-control custom-checkbox custom-control-inline">
<input type="checkbox" id="eduUniversity" name="education[]"
class="custom-control-input" value="university" />
<label class="custom-control-label"for="eduUniversity">university</label>
</div>
</div>
在后端,我使用 laravel 将值存储到数据库,但它运行错误,将数组存储到 mysql 中的字符串。
public function store(Request $request,AdProfile $adprofile)
{
$adprofile->education = $request->education[];
$adprofile->save();
return redirect()->route('adprofile.profilecomplete');
}
你可以使用phpimplode
为此,或者您也可以使用 laravel collection 来实现此目的。
这是例子
collect([1, 2, 3, 4, 5])->implode('-');
// '1-2-3-4-5'
请参阅相关文档Implode https://laravel.com/docs/5.8/collections#method-implode
或者你可以使用 php 函数 implode
see this
$arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr);
//Hello World! Beautiful Day!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)