我有一个 Json 字符串,我正在使用 php 的 json_decode 对其进行解码。
字符串
"address": {
"address": null,
"postalCode": null,
"phoneNumber": "",
"city": null
}
Run Code Online (Sandbox Code Playgroud)
当我解码字符串时,我得到
["address"]=>
array(1) {
["phoneNumber"]=>
string(0) ""
Run Code Online (Sandbox Code Playgroud)
它本质上是用 null 作为值剥离属性,即地址、城市。我能不能防止这种情况发生。
{"cost": null,
"receiptNumber": null,
"receiptType": null,
"labNo": 596726,
"parentLabNo": 0,
"investigation": "BS for mps",
"patient": {
"id": 168967,
"fullName": "UVOGIN",
"dateOfBirth": "1972-04-04 00:00:00",
"gender": "Male"
},
"address": {
"address": null,
"postalCode": null,
"phoneNumber": "",
"city": null
}
}
Run Code Online (Sandbox Code Playgroud) 我试图将错误返回给我的视图,这是我的控制器 TestcategoryController 的一部分
$rules =array(
'name' => 'required'
);
$validator = Validator::make(Input::all(), $rules);
//process
if($validator->fails()){
return Redirect::to('testcategory/create')->withErrors($validator);
}
Run Code Online (Sandbox Code Playgroud)
在视图中,testcategory/create如果我尝试输出类似的错误
@if($errors->any())
{{ $errors->first('name') }}
@endif
Run Code Online (Sandbox Code Playgroud)
我什么也得不到。但如果{{dd($errors)}}我得到
object(Illuminate\Support\ViewErrorBag)#91 (1) { ["bags":protected]=> array(1) {
["default"]=> object(Illuminate\Support\MessageBag)#92 (2)
{ ["messages":protected]=> array(1)
{ ["name"]=> array(1) { [0]=> string(27) "The name field is required." } }
["format":protected]=> string(8) ":message" } } }
Run Code Online (Sandbox Code Playgroud)
我收到错误的唯一方法是杀死脚本。我究竟做错了什么?