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

CookieMonster's avatar

laravel- redirect success message is not showing.

I made a route patch to update profile if user edit it and it will return to the previous page with a success message indicating that it is updating successfully.

My controller:

   
        if ($name && $billTo && $shipTo && $contact) {
            return back()->with(['successful_message' => 'Profile updated successfully'])->with('customerInfo',$customerInfo);
        } else {
            return back()->with(['error_message' => 'Failed to update profile'])->with('customerInfo',$customerInfo);
        }

My blade that will fetch this:


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

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


I can update successfully but there's no message shown to the user. How do I fix this?

0 likes
6 replies
bobbybouwmann's avatar
Level 88

It shouldn't be a problem, but it's better to pass everything as one array

return back()->with([
    'successful_message' => 'Profile updated successfully',
    'customerInfo' => $customerInfo,
]);

Anyway, have you tried redirecting to a certain route instead of using back()? I believe the back() method can have some issues with adding sessions.

return redirect()->route('some.route')->with([
    'successful_message' => 'Profile updated successfully',
    'customerInfo' => $customerInfo,
]);

// Or

return redirect('my/url')->with([
    'successful_message' => 'Profile updated successfully',
    'customerInfo' => $customerInfo,
]);
3 likes
CookieMonster's avatar

Hi,

Yes I tried using redirect as well as back, no luck.

I tried return view itself as well. Should it work if I return as View?

vainway 's avatar

try this up

@if(session('successful_message')))
	<div class="alert alert-success">
		{{ session('successful_message') }}
	</div>
@endif

@if(session('error_message')))
	<div class="alert alert-danger">
		{{ session('error_message') }}
	</div>
@endif
bobbybouwmann's avatar

@niyo What is the difference?

@nickywan123 What happens if you just always display the session variable without the if around it? Does it show up then?

CookieMonster's avatar

Sorry for the late reply, it works but how come if I use

return view(some.view)->with(success message,$xxxx)

does not return the success message?

d-hardin's avatar

This is a year later and I'm sure you've figured it out by now, but I'm assuming your last issue is because you have a space and you're missing quotes around success message

Please or to participate in this conversation.