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

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

    Not a big fan of the solutions provided (ie: multiple variables, helper classes, looping through 'possibly existing variables'). Below is a solution that instead uses an array as opposed to two separate variables. It's also easily extendable to handle multiple errors should you wish but for simplicity, I've kept it to one flash message:

    Redirect with flash message array:

        return redirect('/admin/permissions')->with('flash_message', ['success','Updated Successfully','Permission "'. $permission->name .'" updated successfully!']);
    

    Output based on array content:

    @if(Session::has('flash_message'))
        
    @endif
    

    Unrelated since you might have your own notification method/plugin - but just for clarity - bootstrapNotify is just to initiate bootstrap-notify from http://bootstrap-notify.remabledesigns.com/:

    function bootstrapNotify(type,title = 'Notification',message) {
        switch (type) {
            case 'success':
                icon = "la-check-circle";
                break;
            case 'danger':
                icon = "la-times-circle";
                break;
            case 'warning':
                icon = "la-exclamation-circle";
        }
    
        $.notify({message: message, title : title, icon : "icon la "+ icon}, {type: type,allow_dismiss: true,newest_on_top: false,mouse_over: true,showProgressbar: false,spacing: 10,timer: 4000,placement: {from: "top",align: "right"},offset: {x: 30,y: 30},delay: 1000,z_index: 10000,animate: {enter: "animated bounce",exit: "animated fadeOut"}});
    }
    

提交回复
热议问题