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

mchiasson's avatar

Returning Redirects withMessage not working

Hi everyone, I'm working on a project and can return errors to my views not a problem, however when attempting to return other variables like 'message', it just does not work for me.

I am trying right now on my Post function within a controller to do this

return redirect()->back()->withMessage('IT WORKS!');

and in my view I have

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

            @if(isset($message))
                <div class="alert alert-success">
                    {{ $message }}
                </div>
            @endif 

However when it redirects back to the regular page it does not list my Message. Normal view pages are doing this fine just not on these redirect ones. If I populate an error variable it works fine using

->withErrors('Help this is an Error!')

This is on Laravel 5.4 and I'm not adding the Web middleware onto my routes page. Thanks for any help.

0 likes
6 replies
RamjithAp's avatar

Try this

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

           @if (session('message'))
            <div class="alert alert-success">
            {{ session('message') }}
            </div>
          @endif
mchiasson's avatar

Hi @RamjithAp this did not work for me either. I think the problem is it processes the Controller action for the original GET page which has no 'message' passed to its view.

That seems to be the issue, I need to pass the message variable from my redirect to my original GET controller action and then return that to the view.

Does anyone else have any suggestions? Also @Snapey I tried using their example and it did not work for me.

nanadjei2's avatar
Level 4

Create a file in views. Eg: flashMessage.blade.php. and inside of it do.

   @if (Session::has('success'))
             {{ Session::get('success') }}
    @endif

Now in your controller :

      return back()->with('success', 'Your success message')->withErrors();

In your views :

  @include('flashMessage')
1 like
Snapey's avatar

the example from the docs WILL work (I tested it), but flashed data only persists for one redirect AND provided you have working sessions

Network tools in your browser can be used to check redirects if there is any doubt about how many redirects are occurring.

Also run php artisan route:list and check that web middleware appears against all the relevant routes

mchiasson's avatar

Thanks @Snapey I checked the routes all looked good, still not sure why it won't work as intended.

@nanadjei2 your solution pretty much worked for me.

The below would not work for me on a redirect (please note, this is in my template file and works fine for every other view that gets returned.

@if(isset($message))
                <div class="alert alert-success">
                    {{ $message }}
                </div>
            @endif 

This did work for me! Thanks!

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

Please or to participate in this conversation.