我正在使用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',[AppHttpControllersDoctorAppointmentAppointmentController::class,'AllAppointments'])->name('Appointments')->middleware('doctor');
Route::get('/appointments/view',[AppHttpControllersDoctorAppointmentAppointmentController::class,'ViewAppointment'])->name('Appointment')->middleware('doctor');
Route::post('/appointments/view',[AppHttpControllersDoctorAppointmentAppointmentController::class,'DeleteAppointment'])->name('DeleteAppointment')->middleware('doctor');
Route::get('/appointments/conversation',[AppHttpControllersDoctorAppointmentConversationController::class,'ViewConversation'])->name('ViewConversation')->middleware('doctor');
Route::post('/appointments/conversation',[AppHttpControllersDoctorAppointmentConversationController::class,'SendMessage'])->name('SendMessage')->middleware('doctor');
Route::get('/appointments/requests',[AppHttpControllersDoctorAppointmentAppointmentController::class,'ShowRequest'])->name('Requests')->middleware('doctor');
Route::post('/appointments/requests',[AppHttpControllersDoctorAppointmentAppointmentController::class,'RequestHandel'])->name('Handel')->middleware('doctor');
Route::get('/appointments/prescription',[AppHttpControllersDoctorAppointmentPrescriptionController::class,'CreatePrescription'])->middleware('doctor')->name('CreatePrescription');
Route::post('/appointments/prescription',[AppHttpControllersDoctorAppointmentPrescriptionController::class,'AddMedicine'])->name('AddMedicine');
Route::get('/appointments/checkout',[AppHttpControllersDoctorAppointmentCheckoutController::class,'ViewCheckout'])->middleware('doctor')->name('ViewCheckout');
Route::post('/appointments/checkout',[AppHttpControllersDoctorAppointmentCheckoutController::class,'EndAppointment'])->name('EndAppointment')->middleware('doctor');
}
CheckoutController.php
<?php
namespace AppHttpControllersDoctorAppointment;
use AppHttpControllersController;
use IlluminateHttpRequest;
use IlluminateSupportFacadesDB;
use AppModelsAppointment;
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',[AppHttpControllersDoctorAppointmentCheckoutController::class,'EndAppointment'])->name('EndAppointment')->middleware('doctor');
**
有人能帮我修这个吗
**
;id";字段不是id
,而是appointment_id
。
如果Model::findOrFail()
找不到将转换为404响应的记录,它将抛出异常。
$id = $request->input('appointment_id');
$appointment = Appointment::findOrFail($id);
由于findOrFail而发生错误:您给它的id不正确,因为在表单中您发送了appointment_id
,但您只尝试从请求中检索id
。更改为:
$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');
型号::findOrFail如果找不到id,它将抛出404响应
我希望它能帮助你