【问题标题】:My alert doesn't show up in laravel 5我的警报没有出现在 laravel 5 中
【发布时间】:2016-06-24 13:21:46
【问题描述】:

这是我的 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');
});

任何帮助将不胜感激 提前致谢

【问题讨论】:

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


    【解决方案1】:

    return view('welcome')-&gt;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 的变量,如果是真的,它会打印它的值。

    【讨论】:

      【解决方案2】:

      使用-&gt;with() 将在您的视图中产生一个可访问的变量。

      您可以选择将其更改为:

      @if (isset($info))
      

      但是还有一些其他更简洁的方法可以解决这个问题。

      你可以使用函数withErrors()

      看起来像:

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

      你可以像这样访问它:

      @if ($errors->has('info'))
      
          {{ $errors->first('info') }}
      
      @endif
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-25
        • 1970-01-01
        • 1970-01-01
        • 2016-12-10
        • 2019-06-09
        • 1970-01-01
        相关资源
        最近更新 更多