2017-03-08 14:58
浏览 42

Laravel trait重定向回会话

//inside of trait method
redirect()->back()->with('success', $response['message'])->send();

I have a method inside of trait required redirect back to previous page with flash session, but about code only redirect back without session. (controller call this trait method)

I also tried Session::flash(); but its not working too

this method will use many times in my app, therefor I need this inside of my trait

图片转代码服务由CSDN问答提供 功能建议

redirect() - > back() - > with(  'success',$ response ['message']) - > send(); 

我有一个trait内部的方法需要重定向回到上一页 闪存会话,但关于代码只重定向没有会话。 (控制器调用此特征方法)

我还尝试了 Session :: flash(); 但它也不能正常工作


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doulan0297
    doulan0297 2017-03-08 16:48

    You have to return & I don't know whether you need to use send or not so use like

    return redirect()->back()->with('success', $response['message']);

    See how laravel do it

    点赞 评论
  • douweida2878
    douweida2878 2018-05-28 05:26

    Just return it from trait in your controller, don't use send() method if you want to redirect back with session.

    // In your trait

    return redirect()->back();

    then in your controller,

    $response = mytraitFunction();
    if ($response is instanceof RedirectResponse)
    return $response->with('success', $response['message']);

    Don't forget to use: use Illuminate\Http\RedirectResponse;

    点赞 评论