我的警报没有显示在laravel 5中

时间:2016-06-24 13:21:46

标签: php laravel laravel-5.1 laravel-5.2 blade

这是我的alert.blade.php,

@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');
});

任何帮助都将非常感激 提前致谢

2 个答案:

答案 0 :(得分:0)

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

此行返回&#39; info&#39;的值到视图,并没有将其设置为会话。你的代码:

@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的变量,如果它是真的,它会打印它的值。

答案 1 :(得分:0)

使用->with()会在您的视图中生成一个可访问的变量。

您可以选择将其更改为:

@if (isset($info))

然而,还有一些其他更清洁的方法可以解决这个问题。

您可以使用withErrors()

功能

看起来像是:

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

您可以像访问它一样访问它:

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

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

@endif
相关问题