1

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 know this issue mostly occurs when web middleware is applied twice. I did not apply web middleware twice. It's applied automatically. I shared the code also here.

`public function edit_department(Request $req,$id){
 $dep = department::where("externalid", $id)->first();


        if($req->method() == "POST")
        {
            $req->validate([
                 'inst'=>'nullable|max:2999',
                'name'=>'required|min:3|max:60',
            ]);
    
            try
            {
               
                $dep->name = $req->name;
                $dep->inst= $req->inst;       
                if($dep->save())
                {
                        dispatch(
                            function () use ($this>users)
                            {
                                $this->sendMail($this->users);
                            }
                        )->delay(now()->addSeconds(3);
    
                        return redirect()->back()->with(['bool' => 'true', 'message' => 'Department Updated ']);
                    }
                    else
                    {
                        return redirect()->back()->with(['bool' => '1', 'message' => 'Nothing was changed']);
    
                    }
    
                }
                else
                {
                    return redirect()->back()->with(['bool' => '0', 'message' => 'Department not edit ']);
                }
            
           
        }
    
        return view("departments.update_department");
    
    }`

In blade:

@if(Session::has("bool")) @if(session("bool") == "1") {!! session("message") !!} @elseif(session("bool") == "0") {!! session("message") !!} @endif @endif

Route:

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

This is one function I have given. But the same situation occurs for all the function that flash session message sometime appears and sometime not.

What and where can be the problem. Anyone?

Mehran
  • 282
  • 1
  • 6
  • 17
  • Are you sure you are not hitting the `@elseif` where you are trying to display something from session with a different key. `form_submit_msg` instead of `message`? – geertjanknapen Feb 03 '23 at 12:54
  • sorry that was typing mistake while posting question. So what could be the problem here. – Khizer Rashid Feb 03 '23 at 13:18
  • There are typos galore in your function, and your try block doesn't have a catch to go with it. – aynber Feb 03 '23 at 13:42
  • Even if you did get to the blade, if you set bool, it's either the word `true` or `1`, it's never `0` – aynber Feb 03 '23 at 13:44
  • why do you need to combine 2 routes into one? if get only show view and post will do some logic which you can practically move it to some kind of action or service class – Win Feb 03 '23 at 15:11

0 Answers0