我从Laravel客户端上的RESTfull API获取此错误消息包,我想向用户显示错误,但我很难在包中显示错误一个数组。
nginx root /home/deploy/app_name/public; to /home/deploy/app_name/current/public;
我知道显示错误的常规方法,例如打印性别错误,我可以这样做:
MessageBag {#251 ▼
#messages: array:6 [▼
"gender" => array:1 [▶]
"first_name" => array:1 [▼
0 => "The first name must be at least 2 characters."
]
"last_name" => array:1 [▶]
"user_id" => array:1 [▶]
0 => array:4 [▼
"user_id" => array:1 [▶]
"address2" => array:1 [▼
0 => "The address2 must be at least 3 characters."
]
"zip" => array:1 [▶]
"phone" => array:1 [▶]
]
1 => array:4 [▼
"user_id" => array:1 [▶]
"address2" => array:1 [▼
0 => "The address2 must be at least 3 characters."
]
"zip" => array:1 [▶]
"phone" => array:1 [▶]
]
]
#format: ":message"
}
或检查是否有性别'错误存在:
{!! $errors->first('gender', '<label class="control-label"><i class="glyphicon glyphicon-menu"></i> :message</label>') !!}
但是如何检查数组0是否存在并显示错误?
谢谢:)
答案 0 :(得分:0)
目前在Laravel 5.1打印带有数组的MessageBags是不可行的,我的解决方案是为次要错误实例化不同的MessageBag,而不是将其嵌套在主MessageBag上。
另一个可行的解决方案是将其嵌套:
MessageBag {#251 ▼
#messages: array:6 [▼
"gender" => array:1 [▶]
"first_name" => array:1 [▼
0 => "The first name must be at least 2 characters."
]
"last_name" => array:1 [▶]
"user_id.0.errors" => array:1[▼
0 => "True"
]
"user_id.0.error_name" => array:1[▼
0 => "The first name must be at least 2 characters"
]
"user_id.1.errors" => array:1[▼
0 => "True"
]
"user_id.1.error_name" => array:1[▼
0 => "The first name must be at least 2 characters"
]
]
#format: ":message"
}
但这不是一个干净的方式,正如我所说,创建一个辅助MessageBag更干净,更容易:)