skip to Main Content

I’m using livewire to create a form that a user has to fill up.After that if the form is completed i redirect the user to his dashboard with a session flash.

//CreateAppointment.php
public function createAppointment(){

        $this->insertAppointment();
        session()->flash("appointment", "Your appointment sent successfully!");
        redirect()->to('/dashboard');
        
        $this->sendNotification();
        $this->emit('alert_remove');
    }

And this is how i display that message on the dashboards blade file:

@if(session()->has("appointment"))
    <div id = "ecim" class="alert-success mb-10 border-t-4 border-green-500 rounded-b text-green-900 px-4 py-3 shadow-md" role="alert">
            <div class="flex">
                    <div>
                            <p class="font-bold">{{ session("appointment") }}</p>
                    </div>
            </div>
                </div>
@elseif(session()->has("appointmentError"))
    <div id = "nkEcim" class="alert-error mb-10 border-t-4 border-red-500 rounded-b text-red-900 px-4 py-3 shadow-md" role="alert">
            <div class="flex">
                    <div>
                            <p class="font-bold">{{ session("appointmentError") }}</p>
                        </div>
                </div>
        </div>
@endif

 @push('scripts')
        <script type="text/javascript">
            $(document).ready(function(){
                window.livewire.on('alert_remove',()=>{
                    console.log("test");
                        setTimeout(function(){ 
                            $("#ecim").fadeOut('slow');
                    }, 3000); // 3 secs
                    });
            });
            $(document).ready(function(){
                window.livewire.on('alert_remove_failed',()=>{
                        setTimeout(function(){ 
                            $("#nkEcim").fadeOut('slow');
                    }, 3000); // 3 secs
                    });
        });
        </script>
        @endpush    

Appointment Error message shows up fine but the "appointment" message it doesnt show up.
Checking the developer tools the console.log message also shows up.

2

Answers


  1. The issue is likely caused by the redirect()->to('/dashboard') call in the createAppointment method. When you redirect the user to a new page, the current page is terminated and any data in the session is lost. This means that when the user is redirected to the /dashboard page, the "appointment" session flash is no longer available and is not displayed on the page.

    To fix this issue, you can use the with method on the redirect instance to pass data to the new page. This will allow you to keep the "appointment" session flash data when the user is redirected to the /dashboard page.

    Here’s how you might modify the createAppointment method to pass the "appointment" session flash data to the /dashboard page:

    public function createAppointment()
    {
        $this->insertAppointment();
        session()->flash("appointment", "Your appointment sent successfully!");
    
        // Pass the "appointment" session flash data to the /dashboard page
        redirect()->to('/dashboard')->with(["appointment" => session("appointment")]);
    
        $this->sendNotification();
        $this->emit('alert_remove');
    }
    

    With this change, the "appointment" session flash data will be preserved when the user is redirected to the /dashboard page, and it will be displayed on that page.

    Login or Signup to reply.
  2. //Try to define route in your 
    web.php 
    //where you want to redirect
    
    // Route
    Route::get('dashboard', 
    [Dashboard::class, 'dashboard'])     
    ->name('dashboard');
    
    // In your livewire Component 
    session()->flash('message', 'Your 
    message here');
    
    // Redirect to Dashboard 
    return redirect()                          
    ->route('dashboard');
    
    // In your dashboard view
    @if(Session::has('message')
    <div class='alert alert-success'>
    {{ Session::get('message') }}
    </div>
    @endif
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search