You're browsing the documentation for an old version of Livewire. Consider upgrading your project to Livewire 3.x.
Flash Messages
In cases where it's useful to "flash" a success or failure message to the user, Livewire supports Laravel's system for flashing data to the session.
Here's a common example of its usage:
1use Livewire\Component; 2 3class UpdatePost extends Component 4{ 5 public $post; 6 public $title; 7 8 public function mount(Post $post) 9 {10 $this->post = $post;11 $this->title = $post->title;12 }13 14 public function update()15 {16 $this->post->update([17 'title' => $this->title,18 ]);19 20 session()->flash('message', 'Post successfully updated.');21 }22 23 public function render()24 {25 return view('livewire.update-post');26 }27}
1<form wire:submit.prevent="update"> 2 <div> 3 @if (session()->has('message')) 4 <div class="alert alert-success"> 5 {{ session('message') }} 6 </div> 7 @endif 8 </div> 9 10 Title: <input wire:model="title" type="text">11 12 <button>Save</button>13</form>
Now, after the user clicks "Save" and their post is updated, they will see "Post successfully updated" on the page.
If you wish to add flash data to a redirect and show the message on the destination page instead, Livewire is smart enough to persist the flash data for one more request. For example:
1... 2public function update() 3{ 4 $this->post->update([ 5 'title' => $this->title, 6 ]); 7 8 session()->flash('message', 'Post successfully updated.'); 9 10 return redirect()->to('/posts');11}12...
Now when a user "Saves" a post, they will be redirected to the "/posts" endpoint and see the flash message there. This assumes the /posts
page has the proper Blade snippet to display flash messages.
← Previous Topic
Redirecting
Next Topic →
Nesting Components