php 如何在 Laravel 5.1 中显示保存成功消息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30961631/
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 show Save success message in Laravel 5.1?
提问by Myat Htut
In Laravel 5.1, I can save the data in the database. But I would like to show a success message. How can I do it? My code in Controller save code is
在 Laravel 5.1 中,我可以将数据保存在数据库中。但我想显示一个成功的信息。我该怎么做?我在控制器保存代码中的代码是
public function store(Request $request)
{
$this->validate($request,[
'name'=>'required|unique:seeders|max:255',
'address'=>'required`enter code here`',
'age'=>'required',
]);
$seeder=new Seeders();
$seeder->name=$request->input('name');
$seeder->address=$request->input('address');
$seeder->age=$request->input('age');
$seeder->save();
return redirect()->route("photo.index");
} // save data
回答by Ridwan Pujakesuma
Just adding this code before your redirect code:
只需在重定向代码之前添加此代码:
$request->session()->flash('alert-success', 'User was successful added!');
So the fully code is like here:
所以完整的代码是这样的:
public function store(Request $request)
{
// your function
$request->session()->flash('alert-success', 'User was successful added!');
return redirect()->route("photo.index");
}
Laravel 5.4 about Flash Data: https://laravel.com/docs/5.4/session#flash-data
Laravel 5.4 关于 Flash 数据:https://laravel.com/docs/5.4/session#flash-data
and for your view:
和你的看法:
<div class="flash-message">
@foreach (['danger', 'warning', 'success', 'info'] as $msg)
@if(Session::has('alert-' . $msg))
<p class="alert alert-{{ $msg }}">{{ Session::get('alert-' . $msg) }} <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a></p>
@endif
@endforeach
</div> <!-- end .flash-message -->
You can use Bootstrap Alerts view: http://www.w3schools.com/bootstrap/bootstrap_alerts.asp
您可以使用 Bootstrap Alerts 视图:http: //www.w3schools.com/bootstrap/bootstrap_alerts.asp
回答by Naumov
use the code
使用代码
return redirect()->route("photo.index")->with('message','Success');
and in you template
在你的模板中
@if(session('message'))
{{session('message')}}
@endif
回答by Ikong
Very easy
好简单
public function store(Request $request)
{
$this->validate($request,[
'name'=>'required|unique:seeders|max:255',
'address'=>'required`enter code here`',
'age'=>'required',
]);
$seeder=new Seeders();
$seeder->name=$request->input('name');
$seeder->address=$request->input('address');
$seeder->age=$request->input('age');
$seeder->save();
//PUT HERE AFTER YOU SAVE
\Session::flash('flash_message','successfully saved.');
return redirect()->route("photo.index");
} // save data
In your main view, or index add this.
在您的主视图或索引中添加此项。
@if(Session::has('flash_message'))
<div class="alert alert-success"><span class="glyphicon glyphicon-ok"></span><em> {!! session('flash_message') !!}</em></div>
@endif
If you want to have different flash styles check this out Flash Messages in Laravel 5
如果你想拥有不同的 Flash 风格,请查看Laravel 5 中的 Flash Messages
回答by Viktor
You can redirect user and show him the flash message. The message will be available in view in this way {{ $message }}
您可以重定向用户并向他显示 Flash 消息。消息将以这种方式显示在视图中{{ $message }}
return redirect('user/login')->with('message', 'Success!');
return redirect('user/login')->with('message', 'Success!');
More about this you can find here.
您可以在此处找到更多相关信息。
回答by Mamun Rasid
Show success message with close button:
使用关闭按钮显示成功消息:
Controller code:
控制器代码:
$seeder->save();
return redirect('photo.index')->with('status', 'You have successfully Created!');
Just paste the below code into your view file.
只需将以下代码粘贴到您的视图文件中。
@if (session('status'))
<div class="alert alert-success">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a> {{ session('status') }}
</div>
@endif
回答by omarjebari
In Laravel 5.5 you can use...
在 Laravel 5.5 中,您可以使用...
In your controller action:
在您的控制器操作中:
...
return redirect('photo.index')->with('alert-success', 'The data was saved successfully');
Then in your template you can use the session helper:
然后在您的模板中,您可以使用会话助手:
@if(session()->has('alert-success'))
<div class="alert alert-success">
{{ session()->get('alert-success') }}
</div>
@endif
回答by rothink
the best way -> https://github.com/laracasts/flash
最好的方法 -> https://github.com/laracasts/flash
just do it flash('Message', 'info')
就这样做 flash('Message', 'info')