简体   繁体   English

Laravel Flash Session 有时无法正常工作

[英]Laravel Flash Session does not work expectedly sometime

I am working on a project in which I am using flash session after form submit to display the message.我正在开发一个项目,在提交表单后我使用 flash session 来显示消息。 But the problem is that the flash session message sometimes appears and sometime not.但问题是 flash session 消息有时出现有时不出现。 I shared the code also here.我也在这里分享了代码。

this is the function:这是 function:

public function edit_department(Request $req,$id)
    {
        $dep = department::where("externalid", $id)->first();
        if(!$dep)
        {
            return ['message' => 'Department Not Found'];
        }
        $supervisors = member::select("id","name")->whereRelation("get_role","role_code","=","supervisor_13")->get();

        if($req->method() == "POST")
        {
            $req->validate([
                'name'=>'required|min:3|max:60',
                'supervisor'=>'nullable|exists:members,id',
                'time' =>'required|integer|min:1|max:50000',

                'description'=>'nullable|max:2999',
            ]);

            try
            {
                $sup_temp = $dep->supervisor_id;
                $dep->name = $req->name;
                $dep->ticket_time = $req->time;

                $dep->description = $req->description;
                $dep->supervisor_id = $req->supervisor;
                $desc = "";
                if($dep->save())
                {
                    if($dep->wasChanged())
                    {
                   
                        $desc = "The department ( ".$dep->name." ) has been updated"." by ".session("cms_member_name")." (".session("cms_member_role_name").")";
                        $users = array();
                        $this->mail_subject =" Department Updated";
                        $this->mail_body['description'] = $desc;
                        $this->mail_link = "department/profile";
                        $users = $this->get_mail_members($this->mail_subject, $this->mail_body, $this->mail_link, ['manager_12','super_admin_11'],[],$users);
                       
                        dispatch(
                            function () use ($users)
                            {
                                $this->send_to_members("","","",$users,"false","2");
                            }
                        )->delay(now()->addSeconds(config("app.queue_time")));

                        return redirect()->back()->with(['form_submit_flag' => 'true', 'form_submit_msg' => 'Department Edited Successfully']);
                    }
                    else
                    {
                        return redirect()->back()->with(['form_submit_flag' => 'false', 'form_submit_msg' => 'Nothing was changed']);

                    }

                }
                else
                {
                    return redirect()->back()->with(['form_submit_flag' => 'false', 'form_submit_msg' => 'Department Was not edited Successfully']);
                }
            }
            catch(Exception $ex)
            {
                return redirect()->back()->with(['form_submit_flag' => 'false', 'form_submit_msg' => "An Exception Occured. ".$ex->getMessage()]);
            }
        }

        return view("department.edit_department",compact("supervisors","dep"));

    }

Now This is the blade code:现在这是刀片代码:

@if(Session::has("form_submit_flag"))
@if(session("form_submit_flag") == "true")
<div class="alert alert-success text-dark alert-dismissible">
    <button type="button" class="close" data-dismiss="alert">&times;</button>
    <strong>Message! </strong> {!! session("form_submit_msg") !!}
  </div>

@elseif(session("form_submit_flag") == "false")
<div class="alert alert-danger alert-dismissible">
    <button type="button" class="close" data-dismiss="alert">&times;</button>
    <strong>Message! </strong> {!! session("form_submit_msg") !!}
  </div>


@endif

@endif

And this is the route.这就是路线。

Route::match(['get','post'],'edit_department/{id}',[DepartmentController::class,"edit_department"])->name("edit_department");

Now you can see that I also have queue code to be executed.现在你可以看到我还有队列代码要执行。 Now when I submit the form the flash message in blade sometimes appears and sometime not.现在,当我提交表单时,blade 中的 flash 消息有时会出现,有时不会。 But all code works perfectly.但是所有代码都可以完美运行。 No error occurs.没有错误发生。 Just flash message does not appear.只是 flash 消息没有出现。 Is there any mistake I am doing?我做错了吗?

Any help would be highly appreciated.任何帮助将不胜感激。

Here is the route list这是路线列表

                                        | user_auth                                |
|        | GET|POST|HEAD | edit_department/{id}          | edit_department             | App\Http\Controllers\DepartmentController@edit_department              | web                                      |
|        |               |                               |                             |

在此处输入图像描述

I know this is not your issue, but this could help someone else struggling with similar.我知道这不是您的问题,但这可以帮助其他人遇到类似的问题。

If you've specified a middleware web group on top of the route and it's residing in the web.php file, then the web middleware would be loaded twice which has effects on the session data.如果您在路由顶部指定了一个中间件web组,并且它驻留在 web.php 文件中,那么 web 中间件将被加载两次,这会影响 session 数据。

I'm guess loading any middleware twice would have similar effects.我猜两次加载任何中间件都会产生类似的效果。

See this post for more detail on that issue.有关该问题的更多详细信息,请参阅这篇文章。

https://laracasts.com/discuss/channels/laravel/session-flash-message-not-working-after-redirect-route https://laracasts.com/discuss/channels/laravel/session-flash-message-not-working-after-redirect-route

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM