2016-06-24 20 views
1

這是我alert.blade.php顯示,我的警報不laravel 5

@if(Session::has('info')) 
    <div class="alert alert-info">{{ Session::get('info') }}</div> 
@endif 

,這是我welcome.blade.php,

<!DOCTYPE html> 
<html> 
    <head> 
     <title>Laravel</title> 

     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"  integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7"  crossorigin="anonymous"> 

    </head> 
    <body> 
     @include('alert') 
    </body> 
</html> 

而且,這裏是我的routes.php文件,

Route::get('/', function() { 
    return view('welcome')->with('info','Hello World'); 
}); 

任何幫助將是非常讚賞提前

謝謝

回答

0

return view('welcome')->with('info','Hello World');

此行將'info'的值返回給視圖,並且不會將其設置爲會話。雖然您的代碼爲:

@if(Session::has('info')) 
    <div class="alert alert-info">{{ Session::get('info') }}</div> 
@endif 

檢查會話中是否存在info變量。所以,你需要將視圖更改爲:

@if(isset($info)) 
    <div class="alert alert-info">{{ $info }}</div> 
@endif 

這基本上將檢查視圖有一個名爲info變量,如果這是真的,它打印其價值。

0

使用->with()將在您的視圖中產生一個可訪問的變量。

您可以選擇將其更改爲:

@if (isset($info)) 

但也有一些其他的來解決這個更清潔的方式。

您可以使用該功能withErrors()

它看起來像:

return view('yourView')->withErrors(['info' => 'Your message']) 

,您可以訪問它想:

@if ($errors->has('info')) 

    {{ $errors->first('info') }} 

@endif