我正在做一个项目,在这个项目中,我在表单提交后使用flash会话来显示消息。但问题是flash会话消息有时会出现,有时不会。我在这里也分享了代码。
这是一个函数:
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"));
}
现在这是刀片代码:
@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">×</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">×</button>
<strong>Message! </strong> {!! session("form_submit_msg") !!}
</div>
@endif
@endif
这就是路线。
Route::match(['get','post'],'edit_department/{id}',[DepartmentController::class,"edit_department"])->name("edit_department");
现在你可以看到我还有队列代码要执行。现在当我提交表单时,blade中的flash消息有时会出现,有时不会。但是所有代码都工作得很好。没有错误发生。只是flash消息没有出现。我在做什么错误吗?
任何帮助都将不胜感激。
这是路线表
| user_auth |
| | GET|POST|HEAD | edit_department/{id} | edit_department | App\Http\Controllers\DepartmentController@edit_department | web |
| | | | |
1条答案
按热度按时间xqnpmsa81#
我知道这不是你的问题,但这可以帮助其他人挣扎与类似的。
如果您已经在路由上指定了中间件
web
组,并且它驻留在web.php文件中,那么Web中间件将被加载两次,这对会话数据有影响。我猜加载任何中间件两次会有类似的效果。
请看这篇文章了解更多关于这个问题的细节。
https://laracasts.com/discuss/channels/laravel/session-flash-message-not-working-after-redirect-route