如何在从 Laravel 中的控制器重定向时显示成功消息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29433721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display a success message upon redirection from controller in Laravel?
提问by ikartik90
In my application I have a form that validates and inserts records into the table.
在我的应用程序中,我有一个表单来验证记录并将其插入表中。
Upon successful insertion into the table the controller redirect()
s back to the page ass follows:
成功插入表格后,控制器redirect()
返回页面如下:
return redirect('/');
This works fine, except for the fact that it doesn't tell the user of whether the insertion took place at all or not as only the blank form remains on the page while no success message is displayed.
这工作正常,除了它根本不告诉用户插入是否发生的事实,因为只有空白表单保留在页面上,而没有显示成功消息。
How can I redirect along with data (some form of return code
) so that I can display a simple success message?
如何与数据(某种形式的return code
)一起重定向,以便显示简单的成功消息?
回答by Limon Monte
return redirect('/')->with('message', 'Message sent!');
Read more about redirects in Laravel.
阅读有关Laravel 中重定向的更多信息。
Be sure that Session is working properly to get flash message working.
确保 Session 正常工作以使 Flash 消息正常工作。
回答by Y. Joy Ch. Singha
return redirect('/')->with('success', 'Thanks for contacting us!');
and @include('flash-message')
used this where you want to show your flash message on your page. thanks
并@include('flash-message')
在您想要在页面上显示 Flash 消息的地方使用它。谢谢