Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jpeterson579's avatar

redirect with errors not working...

I am doing a redirect on a form that exists on multiple pages. I want to return them back to the page they filled the form on when there are any errors. Its returning them back to the page just fine however its not returning the errors... Any ideas?

controller

 if($validator->fails()) {
        return Redirect::back()->withErrors($validator)->withInput();
    }

view

 @if(Session::has('error'))
        <script>
            $("#alertmodal").hide().slideDown();
              setTimeout(function(){
                  $("#alertmodal").slideUp();        
              }, 4000);
        </script>
        <div id="alertmsg" class="alert alert-dismissable alert-danger">
              <button class="close" aria-hidden="true" data-dismiss="alert" type="button">×</button> 
              <strong>Error</strong>: {{ Session::get('error') }}
          </div>
    @endif
0 likes
7 replies
bashy's avatar

withErrors() does not put it into the session?

You will need to grab them like this (or loop through with a foreach to display all)

{{ $errors->first('some_field_name') }}

Loop (can't remember the index of it or if it's even needed.

@foreach ($errors->all() as $error)
    {{ $error }}
@endforeach
Harrisonbro's avatar

Related to this, I'm finding that it's not working even when I attempt to output the errors correctly. In my controller I have this (just to test):

public function testAction($id)
{
    $rules = ['foobar' => 'required'];

    $validator = Validator::make(Input::all(), $rules);

    return Redirect::back()->withErrors($validator);
}

And in the view I have:

<pre>
    <?php var_dump($errors) ?>

    @foreach ($errors->all() as $error)
        {{ $error }}
    @endforeach
</pre>

When I view this in an actual browser it shows this:

But the output for that section when checking with Integrated's ->dump() method is this:

<pre>
            object(Illuminate\Support\ViewErrorBag)#278 (1) {
  ["bags":protected]=>
  array(0) {
  }
}

                    </pre>

So it looks like in my case using a redirect withErrors() also isn't working.

Do you have any suggestions of where I should start looking to debug this?

Thanks, Harrison

edoc's avatar

This topic is old.you can access $errorbag anywhere right?

@if($errors->any())
    <ul class="alert alert-danger">
        @foreach ($errors->all() as $error)
            <li >{{ $error }}</li>
        @endforeach
    </ul>
@endif 
saurabhglobus's avatar

return view('welcome')->withErrors([ 'searchError' => 'No jobs found. Please try searching with different criteria', 'type' => 'Advanced Search' ]) works but..

return redirect()->back()->withErrors([ 'searchError' => 'No jobs found. Please try searching with different criteria', 'type' => 'Advanced Search' ]) does not. Any ideas??
saurabhg31's avatar

@EDOC - return view('welcome')->withErrors([ 'searchError' => 'No jobs found. Please try searching with different criteria', 'type' => 'Advanced Search' ]) works but..

return redirect()->back()->withErrors([ 'searchError' => 'No jobs found. Please try searching with different criteria', 'type' => 'Advanced Search' ])

Well I'm using Laravel Framework 5.5.44, & it still doesn't work!

munazzil's avatar

Use as like below in your index.blade.php

@if ($message = Session::get('error'))
    <div class="alert alert-success">
        <p>{{ $message }}</p>
    </div>
  @endif

and controller function redirect

return redirect()->Route('page.index')
                     ->with('error','There was an error');

Please or to participate in this conversation.