如何在Laravel Session :: Flash中获取Flash消息总数

时间:2019-12-13 16:21:20

标签: php laravel

我想知道会话Flash消息的总数。

我已经浏览了Illuminate\Session\Store.php,以查看是否存在某种count()方法。但是我没看到。

这有可能吗?

我问是因为我想创建一个带有徽章的“通知区域”,以显示会话中有多少消息。

  <li class="header">You have {{SESSION_COUNT_HERE}} notifications</li>

以下用于输出单个消息类型的方法,但是我也想遍历每条单独的会话消息并全部显示。

  @foreach (['danger', 'warning', 'success', 'info'] as $msg)
     @if(Session::has($msg))
       <li>
         <a href="#"> {{ Session::get($msg) }} </a>
       </li>
     @endif
  @endforeach

如果我要做这样的事情并添加多条消息。仅存储一条成功消息。我猜没有会话消息数组吗?

        Session::flash('success', 'This is a success 1 test');
        Session::flash('success', 'This is a success 2 test');
        Session::flash('success', 'This is a success 3 test');
        Session::flash('warning', 'This is a  warning test');
        Session::flash('danger', 'This is a  danger test');
        Session::flash('info', 'This is a info test');

        dd(session());

输出显示

#session: Illuminate\Session\Store {#270 ▼
            #id: ""
            #name: "laravel_session"
            #attributes: array:7 [▼
              "_token" => ""
              "_previous" => array:1 [▼
                "url" => "http://mysite.dev"
              ]
              "_flash" => array:2 [▼
                "old" => []
                "new" => array:6 [▼
                  0 => "success"
                  1 => "success"
                  2 => "success"
                  3 => "warning"
                  4 => "danger"
                  5 => "info"
                ]
              ]
              "success" => "This is a success 3 test"
              "warning" => "This is a  warning test"
              "danger" => "This is a  danger test"
              "info" => "This is a info test"
            ]

虽然关键属性“ new”显示正确的计数,但实际上只有最后一条成功消息。

0 个答案:

没有答案