I have tried showing an error message in a Controller and it doesn't work, but when I use dd, it works.
My Code:
if ($validation->fails())
{
    /*Doesn't work
    foreach ($validation->fails() as $messages) {
        $messages // Doesn't work
    }
    */
    dd($validation->errors); //This works
}
                I noticed none of the provided examples here actually work! So here you go. This was my found solution after realizing that validator->messages() returns a protected object which isn't retrievable.
if ($validator->fails())
{
    foreach ($validator->messages()->getMessages() as $field_name => $messages)
    {
        var_dump($messages); // messages are retrieved (publicly)
    }
}
I would reference MessageBag, which is what messages() returns. And for additional acknowledgement of the Validator class - reference this.
$validation->fails() returns a boolean of whether or not the input passed validation. You can access the validation messages from $validation->messages() or pass them to the view where they will be bound to the $errors variable.
See the validator docs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With