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
The issue is likely caused by the
redirect()->to('/dashboard')
call in thecreateAppointment
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: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.