表单提交后 POST 方法返回 404 - Laravel

Posted

技术标签:

【中文标题】表单提交后 POST 方法返回 404 - Laravel【英文标题】:POST method returning 404 after form submission - Laravel 【发布时间】:2021-03-30 07:39:03 【问题描述】:

我正在使用 Laravel 8 开发电子处方应用程序。我构建了一个结帐页面,该页面将提交一个仅包含 1 个值“appointment_id”的表单,以便在单击完成提交表单后,相应的预约状态将被更改由控制器使用约会ID“完成”。但是当我点击按钮触发它给我 404 错误的方法时。我使用过 POST 方法。也使用 CSRF。这是我的代码,

checkout.blade.php

 <form action="/doctor/appointments/checkout" method="POST">
    @csrf
       <div class="form-group row">    
            <div class="col-md-4">            
                  <input type="hidden" name="appointment_id" value="$appointment->id">
                  <input  type="submit" class="btn btn-primary btn-block" value="SAVE">
                       
             </div>
        </div>

   </form>

我的一些路线: web.php

  Route::prefix('/doctor')->name('doctor.')->namespace('Doctor')->group(function()
  //Appointment Routes

      Route::get('/appointments/all',[App\Http\Controllers\Doctor\Appointment\AppointmentController::class,'AllAppointments'])->name('Appointments')->middleware('doctor');
      Route::get('/appointments/view',[App\Http\Controllers\Doctor\Appointment\AppointmentController::class,'ViewAppointment'])->name('Appointment')->middleware('doctor');
      Route::post('/appointments/view',[App\Http\Controllers\Doctor\Appointment\AppointmentController::class,'DeleteAppointment'])->name('DeleteAppointment')->middleware('doctor');
      Route::get('/appointments/conversation',[App\Http\Controllers\Doctor\Appointment\ConversationController::class,'ViewConversation'])->name('ViewConversation')->middleware('doctor');
      Route::post('/appointments/conversation',[App\Http\Controllers\Doctor\Appointment\ConversationController::class,'SendMessage'])->name('SendMessage')->middleware('doctor');
      Route::get('/appointments/requests',[App\Http\Controllers\Doctor\Appointment\AppointmentController::class,'ShowRequest'])->name('Requests')->middleware('doctor');
      Route::post('/appointments/requests',[App\Http\Controllers\Doctor\Appointment\AppointmentController::class,'RequestHandel'])->name('Handel')->middleware('doctor');
 
      Route::get('/appointments/prescription',[App\Http\Controllers\Doctor\Appointment\PrescriptionController::class,'CreatePrescription'])->middleware('doctor')->name('CreatePrescription');
      Route::post('/appointments/prescription',[App\Http\Controllers\Doctor\Appointment\PrescriptionController::class,'AddMedicine'])->name('AddMedicine');
    
      Route::get('/appointments/checkout',[App\Http\Controllers\Doctor\Appointment\CheckoutController::class,'ViewCheckout'])->middleware('doctor')->name('ViewCheckout');
      Route::post('/appointments/checkout',[App\Http\Controllers\Doctor\Appointment\CheckoutController::class,'EndAppointment'])->name('EndAppointment')->middleware('doctor');

CheckoutController.php

<?php

namespace App\Http\Controllers\Doctor\Appointment;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\Appointment;

class CheckoutController extends Controller

    public function ViewCheckout(Request $request)
        $id = $request->input('id');

        $medicines = DB::table('medicines')->where('appointment_id', '=',$id)->get();
        $appointments = DB::table('appointments')->where('id', '=',$id)->get();
        
        return view('doctor.appointments.checkout',['medicines'=>$medicines,'appointments'=>$appointments]);
    

    public function EndAppointment(Request $request)

        $id = $request->input('id');
        $appointment = Appointment::findOrFail($id);

        $appointment->status = 'Completed';
       
        $appointment->save();
       
        return redirect()->to('/doctor/appointments/all')->with('status','Appointment has been completed');
    

我检查了我的路线

php artisan route:list

该路线存在于那里。 我也清除了路线,

php artisan route:clear

仍然面临这个问题。

我还更新了我的作曲家。但这并没有解决我的问题。所有其他路线都运行良好。除了唯一的一条之外,新的路线也在运行:

Route::post('/appointments/checkout',[App\Http\Controllers\Doctor\Appointment\CheckoutController::class,'EndAppointment'])->name('EndAppointment')->middleware('doctor');

**

谁能帮我解决这个问题?

**

【问题讨论】:

findOrFail 将在找不到转换为 404 响应的记录时抛出异常 @lagbox 谢谢:p 我犯了一个愚蠢的错误,将输入字段命名为“appointment_id”并将输入请求为“id”。这就是问题所在。谢谢哥们。 【参考方案1】:

“id”字段不是id,而是appointment_id

Model::findOrFail() 如果找不到将转换为 404 响应的记录,则会引发异常。

$id = $request->input('appointment_id');
$appointment = Appointment::findOrFail($id);

【讨论】:

【参考方案2】:

由于 findOrFail: 你给它一个不正确的 id 而发生错误,因为你在表单中发送了appointment_id,但你只尝试从请求中检索id。将其更改为:

$id = $request->input('appointment_id');
        $appointment = Appointment::findOrFail($id);

【讨论】:

【参考方案3】:

您可以更改代码

 <input type="hidden" name="appointment_id" value="$appointment->id">

 <input type="hidden" name="id" value="$appointment->id">

因为在 CheckoutController 中找不到 id

$id = $request->input('id');

Model::findOrFail 如果没有找到 id 会抛出 404 响应

希望对你有帮助

【讨论】:

以上是关于表单提交后 POST 方法返回 404 - Laravel的主要内容,如果未能解决你的问题,请参考以下文章

js通过生成临时表单再删除的方式向后台提交数据(模拟ajax的post提交但还要跳转页面不返回数据)

提交表单时,DJANGO 中的 POST 方法返回“None”

使用 post 方法提交表单后,表单数据附加到 php 中的 url 为啥?

POST表单提交后django显示消息

关于AsyncHttpClient框架的post 提交表单上传文件怎么弄

jquery 使用post进行提交,但返回的是404错误,这是啥情况呢