muuucho's avatar
Level 11

flash message works only once

If I delete posts without reloading my page the flash message only appears on the first delete. What is the problem? delete() method in app\Livewire\Product.php

public function deleteProduct(int $productId): void
    {
        Product::where('id', $productId)->delete();
        $this->resetPage();
        session()->flash('success-message', 'Post successfully deleted.');
    }

Flash message in view:

@if (session()->has('success-message'))
        <div class="rounded-md bg-green-50 p-4" x-data="{ show: true }" x-show="show" x-init="setTimeout(() => show = false, 3000)">
            <div class="flex">
                <div class="flex-shrink-0">
                    <svg class="h-5 w-5 text-green-400" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
                        <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.857-9.809a.75.75 0 00-1.214-.882l-3.483 4.79-1.88-1.88a.75.75 0 10-1.06 1.061l2.5 2.5a.75.75 0 001.137-.089l4-5.5z" clip-rule="evenodd" />
                    </svg>
                </div>
                <div class="ml-3">
                    <h3 class="text-sm font-medium text-green-800">Success!</h3>
                    <div class="mt-6 text-sm text-green-700">
                        <p>{{ session('success-message') }}</p>
                    </div>

                </div>
            </div>
        </div>
    @endif
0 likes
6 replies
LaryAI's avatar
Level 58

The issue is that the flash message is only displayed once because the Livewire component is not being re-rendered after the delete action. To fix this, you can use Livewire's redirect method to reload the page after the delete action.

Here's an updated version of the deleteProduct method:

public function deleteProduct(int $productId): void
{
    Product::where('id', $productId)->delete();
    session()->flash('success-message', 'Post successfully deleted.');
    $this->redirect(route('products.index'));
}

By calling $this->redirect(route('products.index')), the Livewire component will be re-rendered, and the flash message will be displayed again.

Make sure to replace 'products.index' with the actual route name of the page where the Livewire component is being used.

Also, ensure that you have the necessary route defined in your routes file for the products.index route.

With this change, the flash message should appear every time a post is deleted without reloading the page.

jakubjv's avatar

@Snapey Hello can i have question about that ? .. I installed this and it works perfect if i am removing items or adding something new, messages are showing perfectly. But during redirect i am not able to show success message for login with using flash and i dont know why.. if you can give me advice bout that please :/.

this i have in my login component

<?php

namespace App\Livewire\Forms;

use Livewire\Component;
use Illuminate\Support\Facades\Auth;
use Jantinnerezo\LivewireAlert\LivewireAlert;

class Login extends Component
{
    use LivewireAlert;

    public $email;
    public $password;
    public $remember = false;

    protected $layout = 'components.layouts.app';

    public function render()
    {
        return view('livewire.admin.login');
    }

    public function authenticate()
    {
        $credentials = $this->validate([
            'email' => 'required|email',
            'password' => 'required'
        ],  [
            'email.required' => 'Email je povinné pole.',
            'email.email' => 'Zadejte platný email.',
            'password.required' => 'Heslo je povinné pole.',
        ]);

        if (Auth::attempt($credentials, $this->remember)) {
            session()->regenerate();
            $this->flash('success', 'Uživatel úspěšně přihlášen.', [
                'position' => 'top'
            ]);
            return redirect()->route('reservations');

        } else {
            $this->addError('email', 'Nesprávné údaje pro přihlášení. Email nebo heslo je zadáno špatně.');
        }


    }
}

after redirect message doesnt show up.. if i change it to aler it will show, but just for milisec before redirectin to 'reservations' page :/

muuucho's avatar
Level 11

Thanks, I found this project very helpful: Livewire-alert. It works perfectly with Livewire v3 and it is very easy to install and config.

Please or to participate in this conversation.