Hammad Butt
Hammad Butt

Reputation: 93

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. But the problem is that the flash session message sometimes appears and sometime not. I shared the code also here.

this is the 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. But all code works perfectly. No error occurs. Just flash message does not appear. 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                                      |
|        |               |                               |                             |

enter image description here

Upvotes: 0

Views: 380

Answers (1)

Rager
Rager

Reputation: 876

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.

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

Upvotes: 0

Related Questions