我正在使用引导程序形式从数组中的复选框值中获取输入。我正在使用数组存储复选框值。我如何将这个数组转换为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');}
3 回答
幕布斯7119047
TA贡献1794条经验 获得超8个赞
print_r($request->education); //It is an array print
$str_json = json_encode($request->education); //array to json string conversion
echo $str_json; // printing json string
print_r(json_decode($str_json)); //printing array after convert json string to array
exit; // exiting further execution to check resutls
白板的微信
TA贡献1883条经验 获得超3个赞
在您的“ AdProfile”模型中添加属性强制转换变量,以便laravel将数组自动转换为json并将json转换为数组,
像这样
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class AdProfile extends Model
{
/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts = [
'education' => 'array',
];
}
- 3 回答
- 0 关注
- 1161 浏览
添加回答
举报
0/150
提交
取消