In Laravel, the best way to pass different types of flash messages in the session

前端 未结 16 2706
长情又很酷
长情又很酷 2020-12-12 09:51

I\'m making my first app in Laravel and am trying to get my head around the session flash messages. As far as I\'m aware in my controller action I can set a flash message ei

16条回答
  •  南方客
    南方客 (楼主)
    2020-12-12 10:56

    One solution would be to flash two variables into the session:

    1. The message itself
    2. The "class" of your alert

    for example:

    Session::flash('message', 'This is a message!'); 
    Session::flash('alert-class', 'alert-danger'); 
    

    Then in your view:

    @if(Session::has('message'))
    

    {{ Session::get('message') }}

    @endif

    Note I've put a default value into the Session::get(). that way you only need to override it if the warning should be something other than the alert-info class.

    (that is a quick example, and untested :) )

提交回复
热议问题