我想从用户输入将json数组存储到数据库中。但是,我没有得到理想的结果。以下是代码:
HTML /视图
<div class="form-group" id="InputsWrapper" {{$errors->has('qualification') ? 'has-error': ''}}>
<div class="col-sm-10">
<input class="form-control" type="text" name="qualification[]" id="field_1" value=""><a href="#" class="removeclass"></a> @if($errors->has('qualification'))
<span class="help-block">{{$errors->first('first')}}</span> @endif
</div>
</div>
<div id="AddMoreFileId">
<a href="#" id="AddMoreFileBox" class="btn btn-info ">Add field</a>
<br>
<br>
</div>
控制器
public function store(Request $request)
{
//
$request->validate([
'qualpurpose' => 'required',
]);
$qualpurpose = Qualpurpose::create([
'qualpurpose' => $request->input('qualpurpose'),
'qualification' => json_encode($request->input('qualification')),
'qualificationheader_id'=>session()->get('key')
]);
if ($qualpurpose) {
return redirect()->route('qualpurposes.show', ['qualpurpose' => $qualpurpose->id])
->with('success', 'Purpose created Successfully');
}
return back()->withInput()->with('error' , 'Purpose could not be created');
}
我希望输入以下列格式存储:
{"a":1,"b":2,"c":3,"d":4,"e":5}
但相反,我得到
["adfadsf","bgadf qwe asd","adfadsf ase asdf asder","adfadsf qwer","asdf ffase weww","qwess cfesdf"]
'['方括号而不是普通的花括号。有没有办法可以确保数据存储为普通的json数组而不是方括号?
答案 0 :(得分:0)
如http://php.net/manual/en/function.json-encode.php中所述,您可以使用qf=word
JSON_FORCE_OBJECT
答案 1 :(得分:0)
更改此行:
Quarter breakid freq
Q3 01 1
Q4 01 2
Q4 15 2
要
'qualification' => json_encode($request->input('qualification')),
有关详细信息,请参阅here