从同一个控制器调用函数以返回带有flash消息的视图

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:

  1. index() function execute so the user can show his items.
  2. the user choose an item and save it
  3. save(Request $request) is called
  4. everything save fine
  5. save function call index() with sending a parameter with the flash success message?

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