Php 表单提交后返回404的POST方法-Laravel
我正在使用Laravel 8开发一个电子处方应用程序。我已经建立了一个签出页面,它将提交一个只包含1个值“约会id”的表单,这样在通过单击finish提交表单后,控制器将使用约会id将相应的约会状态更改为“Completed”。但是当我单击按钮触发方法时,它会给我404错误。我使用了POST方法。还使用了CSRF。这是我的密码 checkout.blade.phpPhp 表单提交后返回404的POST方法-Laravel,php,laravel,post,routes,http-status-code-404,Php,Laravel,Post,Routes,Http Status Code 404,我正在使用Laravel 8开发一个电子处方应用程序。我已经建立了一个签出页面,它将提交一个只包含1个值“约会id”的表单,这样在通过单击finish提交表单后,控制器将使用约会id将相应的约会状态更改为“Completed”。但是当我单击按钮触发方法时,它会给我404错误。我使用了POST方法。还使用了CSRF。这是我的密码 checkout.blade.php <form action="/doctor/appointments/checkout" method=
<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>
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');
}
<?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');
}
}
CheckoutController.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>
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');
}
<?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:clear
仍然面临着这个问题
我还更新了我的作曲家。但这并没有解决我的问题。所有其他路线都运转良好。除了唯一的一条路线外,新路线也在运行:
Route::post('/appointments/checkout',[App\Http\Controllers\Doctor\Appointment\CheckoutController::class,'EndAppointment'])->name('EndAppointment')->middleware('doctor');
**
有人能帮我修一下吗?
**由于findOrFail而发生错误:您给它的id不正确,因为您在表单中发送了
约会id
,但您只尝试从请求中检索id
。将其更改为:
$id=$request->input('appointment_id');
$appointment=appointment::findOrFail($id);
该“id”字段不是id
而是appointment\u id
Model::findOrFail()
如果找不到将转换为404响应的记录,将抛出异常
$id = $request->input('appointment_id');
$appointment = Appointment::findOrFail($id);
您可以更改代码
<input type="hidden" name="appointment_id" value="{{$appointment->id}}">
模型::findOrFail如果找不到id,它将抛出404响应
我希望它能帮助您
findOrFail
如果找不到转换为404的记录,就会抛出异常response@lagbox谢谢:p我犯了这样一个愚蠢的错误,将输入字段命名为“约会id”,并请求输入为“id”。。这就是问题所在。谢谢你,伙计。