如何在Laravel中从表单操作URL中移除哈希?

huangapple go评论51阅读模式
英文:

How can I remove hash from form action URL in Laravel?

问题

我有一个带有动作的表单

action="/users#addUserModal"

当表单中存在错误时,在重定向后,Bootstrap 模态框会自动打开。但当没有错误时,模态框仍然显示。

$data = $request->safe()->only(['first_name', 'last_name', 'birth_date', 'username', 'is_admin', 'password']);
$user = User::create($data);
if ($user->exists) {
    session()->flash("success", "User saved");
    return redirect('admin');
}
return back()->with('error', 'User not saved');

如何使重定向到 admin 页面而不带有井号?

英文:

I have form with action

action="/users#addUserModal"

When there is errors in form, after redirect bootstrap modal will open automatically. but when no errors modal is still shown.

$data = $request->safe()->only(['first_name', 'last_name', 'birth_date', 'username', 'is_admin', 'password']);
$user = User::create($data);
if ($user->exists) {
    session()->flash("success", "User saved");
        return redirect('admin');
    }
return back()->with('error', 'User not saved');

how to make this redirect to admin without hashtag?

答案1

得分: 2

protected function getRedirectUrl()
{
return parent::getRedirectUrl() . '#addUserModal';
}

我通过将此代码添加到请求类中修复了它。

英文:
protected function getRedirectUrl()
{
    return parent::getRedirectUrl() . '#addUserModal';
}

i fixed it by adding this to request class.

huangapple
  • 本文由 发表于 2023年6月2日 03:52:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385265.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定