You're browsing the documentation for an old version of Livewire. Consider upgrading your project to Livewire 3.x.
Flash Messages
Introduction
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:
1class UpdatePost extends Component 2{ 3 public Post $post; 4 5 protected $rules = [ 6 'post.title' => 'required', 7 ]; 8 9 public function update()10 {11 $this->validate();12 13 $this->post->save();14 15 session()->flash('message', 'Post successfully updated.');16 }17}
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="post.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:
1public function update() 2{ 3 $this->validate(); 4 5 $this->post->save(); 6 7 session()->flash('message', 'Post successfully updated.'); 8 9 return redirect()->to('/posts');10}
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 →
Traits