PHP
laravel

Laravel5 $validatorとエラー

自分用にメモ

controller
      $validator = $this->validator($request->all(), $validationArray);
      if ($validator->fails()) {
        dd(
          '1)$validator:',$validator
        , '2)$validator->errors():',$validator->errors() //よく使う。
        , '3)$validator->errors()->all():',$validator->errors()->all() //配列になる。あんまり使えない?
        , '4)$validator->errors()->first("number"):',$validator->errors()->first('number') //viewにエラーメッセージ出す用
        , '5)$validator->messages():',$validator->messages() 
        );
      }

結果

"1)$validator:"
Validator {#294 ▼
  #translator: Translator {#298 ▶}
  #presenceVerifier: DatabasePresenceVerifier {#297 ▶}
  #container: Application {#3 ▶}
  #failedRules: array:2 [▶]
  #messages: MessageBag {#300 ▶}
  #data: array:13 [▶]
  #files: []
  #rules: array:3 [▶]
  #after: []
  #customMessages: []
  #fallbackMessages: []
  #customAttributes: []
  #customValues: []
  #extensions: []
  #replacers: []
  #sizeRules: array:4 [▶]
  #numericRules: array:2 [▶]
  #implicitRules: array:8 [▶]
}


"2)$validator->errors():"
MessageBag {#300 ▼
  #messages: array:2 [▼
    "number" => array:1 [▼
      0 => "「電話番号」は必須です"
    ]
    "email" => array:1 [▼
      0 => "「Email」は必須です"
    ]
  ]
  #format: ":message"
}


"3)$validator->errors()->all():"
array:2 [▼
  0 => "「電話番号」は必須です"
  1 => "「Email」は必須です"
]


"4)$validator->errors()->first("number"):"
"「電話番号」は必須です"


"5)$validator->messages():"
MessageBag {#300 ▼
  #messages: array:2 [▼
    "number" => array:1 [▼
      0 => "「電話番号」は必須です"
    ]
    "email" => array:1 [▼
      0 => "「Email」は必須です"
    ]
  ]
  #format: ":message"
}