rikw's avatar
Level 1

Flash messages in Livewire

Hi all,

By no way can I get flash messages to work in my Lirewire components. Some posts say it cannot be done, others give code sample that do not do anything (for me). Who has the definitive answer?

Basically I have a form and a list component. After saving the form I want to redirect to the list and show a flash message that the form was successfully saved. Who can help me out?

Cheers Rik

0 likes
3 replies
LaryAI's avatar
Level 58

Certainly! Here’s the definitive way to show flash messages in Livewire after submitting a form and redirecting to a list component.

1. In your Form Livewire component:

After saving the record, use session()->flash() and redirect:

public function save()
{
    // Your save logic here...

    session()->flash('success', 'Form was successfully saved!');

    return redirect()->route('your.list.route'); // Replace with your actual route
}

2. In your List Blade template (where you redirect):

Add code to display the flash message:

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

Important Notes:

  • Flash messages work on full redirects (which reload the page). If your list is a different Livewire component/page, make sure you use a redirect, not just emitting events.
  • Flash messages won’t automatically show if you update the list via an AJAX Livewire update. In that case, use Livewire events instead.

For single-page (no redirect) Livewire:
If you want to show a message in the same component after save (without redirect):

Blade view:

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

Component:

public $successMessage;

public function save()
{
    // Save logic...

    $this->successMessage = 'Form was successfully saved!';
}

Summary:

  • Use session()->flash() for classic flash messaging on redirects.
  • Use a public property for instant, no-redirect messaging.

Let me know if you need an event-based Livewire solution or have further questions!

rikw's avatar
rikw
OP
Best Answer
Level 1

This was what I was doing already. After waisting half a day on this I just saw that my list component included #[Lazy(isolate: false)]. After removing this my flash message worked straight away.

bvfi-dev's avatar

The thing with flash messages is you have to consider 2 scenarios when using Livewire:

  • One where you want instant notifications as soon as the button is clicked
  • One where there is a redirect in the session

I like making my stuff custom so that I always have 100% control over it, and also from my experience you need separate handles for both. I have for example an instant flash notification that appears on a click of a button that I call via Livewire's dispatch().

However If I redirect it would disappear so I need a session flash for after the session has been redirected and depending on your scenario you need to use one of these options.

Lets say I have a Livewire Index component and within it, I have create modal. What I would do on submitting the form is show a flash notification and make the modal not show -> User clicks "Submit" modal closes, notification is shown with results success/fail. This creates a smooth UI experience. However If I were to want to redirect to the edit page after submitting the create form, I would need a session flash notification.

The instant notification banner I make like so:

  1. In my default app layout, I insert the livewire component:
@stack('modals')
@livewire('components.notification-manager')

Then my component is like:

This way I can have multiple notifications stack on each other, I can give custom timeouts, I set the type, etc

Now from anywhere in the Livewire Component I can call:

$this->dispatch('show-banner', message: 'Success message' , type: 1);

And this will make the notification appear The frontend:

<div class="w-full fixed mx-auto top-2 right-4 flex flex-col space-y-2" style="z-index: 9999 !important;">
    @foreach ($banners as $banner)
        <div wire:key="banner-{{ $banner['key'] }}" x-data="{ show: true }" x-init="setTimeout(() => show = false, {{ $banner['timeout'] }})" x-show="show"
             x-on:click="show = false"
...

You can use alpineJs to make it appear from top with smooth transitions and animations and you also need to display the $banner['message']

And depending on type I give it a different background color.

With session banner just make a blade component: , put it in your app layout, or the main html file you want it to be used, and have the html like so:

@props(['style' => session('flash.bannerStyle', 'success'), 'message' => session('flash.banner')])

<div x-data="{{ json_encode(['show' => true, 'style' => $style, 'message' => $message]) }}"
    :class="{ 'bg-green-500': style == 'success', 'bg-red-700': style == 'danger', 'bg-yellow-500': style == 'warning', 'bg-gray-500': style != 'success' && style != 'danger' && style != 'warning'}"
            style="display: none;"
            x-show="show && message"
            x-on:banner-message.window="
                style = event.detail.style;
                message = event.detail.message;
                show = true;
            ">
...

Remember to use x-text="message" to display the message in a <p> or something, and don't forget the x-on:click="show = false" so that the notification can be closed.

Now anywhere in your livewire components you can do:

return redirect()->route('route.name')
				->with('flash.banner', $message)
				->with('flash.bannerStyle', 'success');

And when you return, the banner will pop out. I dont have full code for you, because mine has a lot more code in it and It took me a long time to figure it out. Im giving you an idea and more than a start to learn how the notifications should work

Please or to participate in this conversation.