skip to Main Content

I’m calling this:

request()->validate($rules);

If there is an error, then Laravel will flash the errors to the session and return to the previous page.

How do I get the errors that Laravel flashed to session from within PHP?

I am not referring to getting errors in a Blade template, or using a Blade template directive.

For example, how could I get errors if I was coding within a class file.

2

Answers


  1. Chosen as BEST ANSWER

    I found out how to do it

    $viewErrorBag = session()->get('errors');
    $bag = $viewErrorBag->getBag('default');
    $errors = $bag->all()
    

  2. Well i dont really understand what you mean by getting the errors from within PHP code.

    Try This if it would help

    @if ($errors->any())
    <div class="alert alert-danger">
        @foreach ($errors->all() as $error)
            <strong><li>{{ $error }}</li></strong>
        @endforeach
    </div>
    @endif
    

    Or Explain your question better if that doesnt help

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search