I'm trying to implement Ajax call in Laravel project. Currently I'm looking for a way to display success/error messages after Ajax successfully execute something & redirect the page. In Laravel controller, usually I can do it like this:
if($patient->wasRecentlyCreated){
return redirect('/patients')->with('success','New patient data saved');
} else {
return redirect('/patients')->with('error','Patient already exist');
}
As in the blade view, I have added the message section in the layout page (so that the message will appear on the top of redirected page) :
@if(count($errors)>0)
@foreach ($errors->all() as $error)
<div class="alert alert-danger">
{{$error}}
</div>
@endforeach
@endif
@if(session('success'))
<div class="alert alert-success">
{{session('success')}}
</div>
@endif
@if(session('error'))
<div class="alert alert-danger">
{{session('error')}}
</div>
@endif
The case with Ajax call, I'm able to pass the redirect target URL from Laravel controller but I don't know how to pass the success/error message to Ajax. Below is the sample of Laravel controller I have made to handle the Ajax:
if($settlement->wasRecentlyCreated){
return url('/patients');
}
And in Ajax section, I can redirect using window.location :
$.ajax({
url: "/createsettlement",
type: 'post',
data: {
billing_id : billing_id,
prices : prices,
_token : '{{csrf_token()}}'
},
success: function (data) {
console.log(data);
window.location = data;
},
Any idea how to pass the success/error messages while Ajax do page redirection?
OK. you can do the redirection and show in the same time.
But considering that ajax is JS you can set a timeout for redirection, i.e.
$('#error-alert').text('The text you want to display').show();
setTimeout(() => {
window.location = url;
}, 3000); //this case waits 3 secs to redirect to the page you want
Hope it helps
The redirect('/page')->with('name', $data)
method flashes session data to the currently rendered view. To create persistent session data, use session(['name'=>$data])
instead before the redirect.
Implement it in your code like this:
if($patient->wasRecentlyCreated){
session(['success'=>'New patient data saved']);
return redirect('/patients');
} else {
session(['error'=>'Patient already exist']);
return redirect('/patients');
}
Now, you don't have to worry about the session data being available after a JavaScript redirect. Just ensure you retrieve the session data in which ever page the redirect url is pointing to.
Also, if you want to simulate the default flash behavior you can remove the session data after retrieving them in your blade file like this
@if(count($errors)>0)
@foreach ($errors->all() as $error)
<div class="alert alert-danger">
{{$error}}
</div>
@endforeach
@endif
@if(session('success'))
<div class="alert alert-success">
{{session('success')}}
</div>
@endif
@if(session('error'))
<div class="alert alert-danger">
{{session('error')}}
</div>
@endif
@php
if(session('success'))
session()->forget('success');
if(session('error'))
session()->forget('error');
@endphp