2016-09-27 25 views
0

多次以我的控制器:Laravel會話閃光消息出現在pageback

Session::flash('created', "Student created"); 
return Redirect::to('student'); 

筆者認爲:

@if(Session::has('created')) 
alert('updated'); 
@endif 
+0

你的問題到底是什麼? – Nil

+0

爲什麼你使用session?你可以使用Redirect :: route()方法作爲更好的解決方案。 –

回答

0

顯示會話消息

@if(Session::has('success')) 
    <div class="alert alert-success alert-dismissable alert-box"> 
    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button> 
    {{ Session::get('success') }}    
    </div> 
@endif 
@if(Session::has('error')) 
    <div class="alert alert-danger alert-dismissable alert-box"> 
    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button> 
    {{ Session::get('error') }} 
    </div> 
@endif 

從控制器調用

return redirect()->back()->with('success', 'Data added successfully'); 
+0

警報顯示多次當我在browesr中按下來回按鈕 –

0

請避免不必要的會話使用。您可以使用Redirect::route。檢查以下示例:

1)將$strChecked = false;初始化爲您的學生路線控制器,然後將$strChecked = true;設置爲您的學生更新方法。

2)更換與您的看法return \Redirect::route('student')->with('strChecked', $strChecked);

3)然後你回線,您可以使用:

@if($strChecked) 
    alert('updated'); 
@endif 

EDITS:

讓我說明我的工作演示代碼:

1)ROUTE:

Route::get('flash', '[email protected]')->name('frontend.flash'); 
Route::post('flash/update', '[email protected]')->name('frontend.flash.update'); 

2)查看:

@section('content') 
{{ Form::open(['route' => 'frontend.flash.update', 'class' => 'form-horizontal']) }} 

{{ Form::submit('Demo', ['class' => 'btn btn-primary', 'style' => 'margin-right:15px']) }} 

{{ Form::close() }} 

@endsection 
@section('after-scripts-end') 
    <script> 
     @if(Session::has('created')) 
     alert('updated'); 
     @endif 
    </script> 
@stop 

3)控制器:

//Method to display action... 
public function flashIndex() 
{ 
return view('frontend.flash'); 
} 

//Method to update action... 
public function studentUpdate() 
{ 
Session::flash('created', "Student created"); 
return Redirect::to('flash'); 
} 

希望這會清除你的疑慮。

上面的示例在我的結尾正常工作。如果這不適合你,請描述你的代碼。

+0

對不起,沒有能夠在視圖中獲得$ strChecked值。 –

+0

請指定您使用此代碼時遇到的錯誤? –

+0

$ strChecked的值未定義,因爲它寫在jquery或用戶的任何地方頁面內顯示未定義 –