dongxuanjiao0795 2017-02-06 16:25
浏览 57
已采纳

从同一个控制器调用函数以返回带有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?

  • 写回答

1条回答 默认 最新

  • doushu9253 2017-02-06 16:30
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部