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

前端 未结 16 2769
长情又很酷
长情又很酷 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:33

    For my application i made a helper function:

    function message( $message , $status = 'success', $redirectPath = null )
    {
         $redirectPath = $redirectPath == null ? back() : redirect( $redirectPath );
    
         return $redirectPath->with([
             'message'   =>  $message,
             'status'    =>  $status,
        ]);
    }
    

    message layout, main.layouts.message:

    @if($status)
       
    {{ $message }}
    @endif

    and import every where to show message:

    @include('main.layouts.message', [
        'status'    =>  session('status'),
        'message'   =>  session('message'),
    ])
    

提交回复
热议问题