来自我的PostController的方法
public function store(PostRequest $request)
{
if (Post::create($request->all())) {
$request->session()->flash('status', 'Post was successfully added!');
} else {
$request->session()->flash('status', 'Error!');
}
return redirect('/');
}
索引视图
<?php if(session()->has('status')){
echo '<div style="text-align: center">';
echo session()->get('status');
echo '</div>';
}?>
如何以不同方式显示错误和成功消息?
答案 0 :(得分:19)
您可以使用以下内容:
public function store(PostRequest $request) {
if (Post::create($request->all())) {
$request->session()->flash('message.level', 'success');
$request->session()->flash('message.content', 'Post was successfully added!');
} else {
$request->session()->flash('message.level', 'danger');
$request->session()->flash('message.content', 'Error!');
}
return redirect('/');
}
在您的刀片文件中:
@if(session()->has('message.level'))
<div class="alert alert-{{ session('message.level') }}">
{!! session('message.content') !!}
</div>
@endif
上面的代码使用bootstrap's alert css classes进行样式设置,其灵感来自Jeffrey Way's Laracasts Flash package
答案 1 :(得分:10)
您可以这种方式显示Flash消息:
在您的控制器中:
$request->session()->flash('success', 'Record successfully added!');
//OR
$request->session()->flash('warning', 'Record not added!');
在你的视线中:
@foreach (['danger', 'warning', 'success', 'info'] as $key)
@if(Session::has($key))
<p class="alert alert-{{ $key }}">{{ Session::get($key) }}</p>
@endif
@endforeach
答案 2 :(得分:1)
您可以在控制器中使用Category::setBindings([$old_hierarchy, $new_hierarchy, now()])
->whereRaw("hierarchy LIKE ?", [$old_hierarchy . '>%'])
->update([
'hierarchy' => DB::raw("REPLACE(hierarchy, ?, ?)"),
'updated_at' => DB::raw("?")
]);
->with()
您的视图可能如下所示:
public function store(PostRequest $request) {
if (!Post::create($request->all())) {
return redirect('/')->with('error', 'Error!');
}
return redirect('/')->with('success', 'Post was successfully added!');
}
或(带有@if (session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
@endif
@if (session('error'))
<div class="alert alert-danger">
{{ session('error') }}
</div>
@endif
类的附加CSS):
alert-error
答案 3 :(得分:0)
在Blade模板中编写如下代码:
sensor.ID := bson.NewObjectId()
在您的控制器中编写如下代码:
@if(Session::has('error'))
<p class="alert alert-danger">{{ Session::get('error') }}</p>
@endif
答案 4 :(得分:-1)
@if(session('success'))
<div class="alert alert-success" id="id">
{{session('success')}}
</div>
@endif
@if(session('danger'))
<div class="alert alert-danger" id="id">
{{session('danger')}}
</div>
@endif
<script>
$('#id').delay(1000).fadeOut('fast');
</script>
@yield('content')