将 Laravel $validation->messages() 转换为对象数组的优雅代码

Evg*_*niy 4 php laravel laravel-5

通常,在 Laravel 中验证表单请求时,我们可以使用 $validation->messages() 访问错误。例如:

object(Illuminate\Support\MessageBag)#184 (2) { ["messages":protected]=> array(2) { ["email"]=> array(1) { [0]=> string(40) "The email must be a valid email address." } ["password"]=> array(2) { [0]=> string(41) "The password confirmation does not match." [1]=> string(43) "The password must be at least 6 characters." } } ["format":protected]=> string(8) ":message" }. 
Run Code Online (Sandbox Code Playgroud)

是否有一些优雅的方法将对象 MessageBag 转换为示例数组,例如:

[
object({"email" => "The email must be a valid email address."}),
object({"password" => "The password confirmation does not match."})
...
]
Run Code Online (Sandbox Code Playgroud)

PS:如果在 MessageBag 中,任何字段有多个项目,我只想要结果对象数组中的第一项。

提前致谢。

Mar*_*ean 7

$validation->messages()->all();
Run Code Online (Sandbox Code Playgroud)