When the user save the object in the blade view, a function into my controller execute with post request. If the save was success I want to reload the page with the changes.
So I need to call index() again to get the refreshed data. Also, I want to show a success message. So the steps are:
I have this structure in my controller:
public function index(){
...
return view('agenda')->with(['agenda'=>$response]);
}
public function save(Request $request){
...
// here where I need to return index function with success message
}
How can I solve my problem?
Assuming the save
action was called from the index page, simply redirect back
public function save(Request $request){
...
return redirect()->back()->with('message', 'Item saved');
}
If it was called from a different page, redirect to index page. Assuming your index()
function matches a route /
:
return redirect()->to('/')->with('message', 'Item saved');
Then display the flash message
in your index view.
@if(Session::get('message'))
{{ Session::get('message') }}
@endif
For styling, if you are using bootstrap you can do
@if(Session::get('message'))
<div class="alert alert-success">
{{ Session::get('message') }}
</div>
@endif