【问题标题】:POST method returning 404 after form submission - Laravel表单提交后 POST 方法返回 404 - 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”。这就是问题所在。谢谢哥们。

标签: php laravel post routes http-status-code-404


【解决方案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 响应

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 2020-04-07
        • 2017-10-07
        • 2012-03-06
        • 2019-12-08
        • 2018-10-08
        • 1970-01-01
        • 1970-01-01
        • 2019-11-04
        • 2020-12-21
        相关资源
        最近更新 更多