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

POST method returning 404 after form submission - Laravel

我正在使用 Laravel 8 开发电子处方应用程序。我已经建立了一个结帐页面,它将提交一个仅包含 1 个值“appointment_id”的表单,以便在通过单击完成提交表单后,相应的预约状态将由控制器使用 appointment_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:您给它的 ID 不正确,因为在表单中您发送了 appointment_id 但您只尝试从请求中检索 id。将其更改为:

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

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

如果找不到将转换为 404 响应的记录,

Model::findOrFail() 将抛出异常。

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

您可以更改密码

 <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 响应

希望对你有所帮助