dongxuanjiao0795 2017-02-07 00: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-07 00: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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题