public function store(Request $request){
$this->validate($request,array(
'title'=>'required|max:255',
'slug' =>'required|alpha_dash|min:5|max:255|unique:posts,slug',
'body' =>'required'
));
//store in the database
$post = new Post;
$post->title = $request->title;
$post->slug = $request->slug;
$post->body = $request->body;
$post->save();
//This code will generate flash message about success or failure about data insert
Session::flash('success', 'Data has been saved successfully!');
//Redirect to another page
return redirect()->route('posts.show', $post->id);
}
@if(Session::has('success'))
<div class="alert alert-success" role= "alert">
<strong>Successful:</strong>
{{ Session::get('success') }}
</div>
@endif
@if(count($errors) > 0)
<div class="alert alert-danger" role="alert">
<strong>Errors:</strong>
<ul>
@foreach($errors as $error)
<li> {{ $error }} </li>
@endforeach
</ul>
</div>
@endif
上面的代码没有显示任何Flash消息。但当 Session :: flash(&#39;成功&#39;,&#39;数据已成功保存!&#39;); 写作: Session :: put(&#39;成功&#39;,&#39;数据已成功保存!&#39;);
显示flash消息但不会消失。
Route :: group([&#39; middleware&#39; =&gt; [&#39; web&#39;]],function(){
Route::get('auth/login', ['as' =>'login', 'uses'=>
'Auth\AuthController@getLogin']);
Route::post('auth/login', 'Auth\AuthController@postLogin');
Route::get('auth/logout', ['as' => 'logout', 'uses' =>
'Auth\AuthController@getLogout']);
Route::get('auth/register','Auth\AuthController@getRegister');
Route::post('auth/register','Auth\AuthController@postRegister');
Route::get('password/reset/{token?}',
'Auth\PasswordController@showResetForm');
Route::post('password/email',
'Auth\PasswordController@sendResetLinkEmail');
Route::post('password/reset','Auth\PasswordController@reset');
Route::get('contact', 'PagesController@getContact');
Route::get('about', 'PagesController@getAbout');
Route::get('/', 'PagesController@getIndex');
Route::get('reader/{slug}', ['as' => 'reader.single', 'uses' =>
'ReaderController@getSingle'])
->where('slug', '[\w\d\-\_]+');
Route::get('reader', ['as' => 'reader.index', 'uses' =>
'ReaderController@getIndex' ]);
Route::resource('posts', 'PostController');
});
请帮忙!
答案 0 :(得分:-1)
1.-确保您是否在视图中包含消息模板,在本例中为SHOW视图 2.-尝试替换您的消息模板:
@if(Session::has('success'))
<div class="alert alert-success" role= "alert">
<strong>Successful:</strong>
{!! session('success') !!}
</div>
@endif
@if(count($errors) > 0)
<div class="alert alert-danger" role="alert">
<strong>Errors:</strong>
<ul>
@foreach($errors as $error)
<li> {{ $error }} </li>
@endforeach
</ul>
</div>
@endif
看,我正在使用{!!会话(&#39;成功&#39;)!!}而不是你的