try this
->withErrors('message', 'There was a failure while sending the message!');
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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">×</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">×</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">×</span>
</button>
</div>
</div>
@endif
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">×</span>
</button>
</div>
</div>
@endif
Please or to participate in this conversation.