Laravel路由错误:此路线不支持GET方法

发布于 2025-01-22 15:03:32 字数 1270 浏览 3 评论 0原文

谁能帮助我在路线中找到确切的错误?

symfony \ component \ httpkernel \ exception \ methodnotloweredhttpexception 对于此路线,不支持GET方法。支持的方法:发布。 路由:: get('/outh'[app \ http \ controllers \ leavecontroller :: class,'well'']);

Route::post('/save',[App\Http\Controllers\LeaveController::class,'saveRecord'])
    ->name('save');

<form action="{{ route('save') }}" method="post">

控制器

public function saveRecord(Request $request)
{
    $request->validate([
        'fromdate' => 'required|string|max:255',
        'todate' => 'required|string|max:255',
        'leave_reason' => 'required|string|max:255',
    ]);

    DB::beginTransaction();
    try {
        $Leaves = new Leaves;
        $Leaves->rec_id = $request->rec_id;
        $Leaves->fromdate = $request->fromdate;
        $Leaves->todate = $request->todate;
        $Leaves->leave_reason = $request->todate;
        $Leaves->save();

        DB::commit();

        redirect()->back()->with('success', 'Add leaves successfully.');
    } catch (\Exception $e) {
        DB::rollback();

        return redirect()->back()->with('error', 'Add leaves fail');
    }
}

Can anyone please help me find the exact error in the route?

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The GET method is not supported for this route. Supported methods: POST.
Route::get('/leave'[App\Http\Controllers\LeaveController::class,'leave']);

Route::post('/save',[App\Http\Controllers\LeaveController::class,'saveRecord'])
    ->name('save');

<form action="{{ route('save') }}" method="post">

Controller

public function saveRecord(Request $request)
{
    $request->validate([
        'fromdate' => 'required|string|max:255',
        'todate' => 'required|string|max:255',
        'leave_reason' => 'required|string|max:255',
    ]);

    DB::beginTransaction();
    try {
        $Leaves = new Leaves;
        $Leaves->rec_id = $request->rec_id;
        $Leaves->fromdate = $request->fromdate;
        $Leaves->todate = $request->todate;
        $Leaves->leave_reason = $request->todate;
        $Leaves->save();

        DB::commit();

        redirect()->back()->with('success', 'Add leaves successfully.');
    } catch (\Exception $e) {
        DB::rollback();

        return redirect()->back()->with('error', 'Add leaves fail');
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪 2025-01-29 15:03:32

您将请求方法定义为以下“帖子”,这意味着它仅接受发布请求。如果您尝试从浏览器中走这条路线,最终将出现您提到的错误。如果您更改帖子以使它起作用。

如果您从表格或AJAX请求向该路线提出请求,只需将请求方法更改为“发布”

路由:: post ('/save',[app \ http \ controllers \ leavecontroller :: class,'saverecord'])) - &gt; name('save'');

You defined the request method as "POST" below, which means it only accepts post requests. If you try to go that route from the browser, it gonna end up with that error you mentioned. If you change the post to get it gonna work.

If you making a request to that route from forms or ajax request, just change request method to "POST"

Route::post('/save',[App\Http\Controllers\LeaveController::class,'saveRecord'])->name('save');

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文