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

AquinoBR's avatar

Return success message

I have a form on the site and after sending the message I want to present a message of success or failure. But the message is not coming back.

condition in function


if ($mail)
            return redirect()
                        ->back()
                        ->with('success', 'Your message has been sent successfully!');

            return redirect()
                        ->back()
                        ->withInput()
                        ->with('error', 'There was a failure while sending the message!');

alert include

@if ($errors->any())
    <div class="col-sm-12">
        <div class="alert  alert-warning alert-dismissible fade show" role="alert">
            @foreach ($errors->all() as $error)
                <span><p>{{ $error }}</p></span>
            @endforeach
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
        </div>
    </div>
@endif

@if (session('success'))
    <div class="col-sm-12">
        <div class="alert  alert-success alert-dismissible fade show" role="alert">
            {{ session('success') }}
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
        </div>
    </div>
@endif

@if (session('error'))
    <div class="col-sm-12">
        <div class="alert  alert-danger alert-dismissible fade show" role="alert">
            {{ session('error') }}
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
        </div>
    </div>
@endif
0 likes
14 replies
rin4ik's avatar

try this

 ->withErrors('message', 'There was a failure while sending the message!');
1 like
AquinoBR's avatar

@rin4ik You have not returned any messages. One note is that the user is not logged in.

bobbybouwmann's avatar

Please use {, } because it makes your code a lot clearer!

if ($mail) {
    return redirect()
        ->back()
        ->with('success', 'Your message has been sent successfully!');
}

return redirect()
    ->back()
    ->withInput()
    ->with('error', 'There was a failure while sending the message!');

So this bit of code is not working for you? Do you get redirected back?

2 likes
Snapey's avatar

Install debugbar

As well as lots of useful information, it will show what is in session

1 like
AquinoBR's avatar

@bobbybouwmann

I added {,} ... yes everything is working the message is being sent successfully and returns to page. Only the success or error message does not appear.

Snapey's avatar

So it could be an issue with the view then, or even just css? (debugbar would confirm this)

view the html source in your browser and check for ypur messages

2 likes
bobbybouwmann's avatar
Level 88

Well, when you use with it will be returned as a variable and not as a session variable! Instead you need to do this

return redirect()
    ->back()
    ->withInput()
    ->with('error', 'There was a failure while sending the message!');

@if (isset($error))
    <div class="col-sm-12">
        <div class="alert  alert-danger alert-dismissible fade show" role="alert">
          $error
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
        </div>
    </div>
@endif
1 like
bobbybouwmann's avatar

If you want to use a session you need to do something like this

session()->put('error', 'There was a failure while sending the message!');

return redirect()
    ->back()
    ->withInput();

Now you can access it like a session in your view

@if (session()->has('error'))
    {{ session('error') }}
@endif
1 like
AquinoBR's avatar

Thank you my friends for your attention and patience. I found the problem, it was in the alert css.

@bobbybouwmann It worked out right now I checked your code and removing the css appeared.

Thank you all!

AquinoBR's avatar

One last thing, if you can help. I have seen that now the message is not disappearing after refreshing the page.

Vilfago's avatar

Use session->flash instead of session->put. It will put it in the session only for the next request.

1 like

Please or to participate in this conversation.