Laravel 运行 来自 blade 按钮的功能
Laravel running function from button in blade
我在获取 blade 文件中的按钮以正确 运行 控制器中的功能时遇到问题,当我单击该按钮时,我得到一个 methodnotallowedexception 并且无法确定找出设置不正确的地方
据我所知,这就像我为不同的 blade 和控制器设置的另一个按钮一样,除非我忽略了一些东西,这就是我在这里的原因。如果您需要其他代码,请告诉我。
首先是我的路线:
Route::post('/viewPatient/{user}/discharge', 'PatientController@discharge')->name('patients.discharge');
Route::post('/viewPatient/{user}/reAdmitt', 'PatientController@reAdmitt')->name('patients.reAdmitt');
Route::post('/viewPatient/{user}/reAdmitted', 'PatientController@reAdmitted')->name('patients.reAdmitted');
接下来是控制器功能
public function discharge(User $user)
{
$user->discharged = true;
$user->discharged_date = now();
$user->current_facility_id = null;
$user->save();
// find all the users documents that are not historical
$documents = Document::where('user_id', $user->id)->where('historical', false)->get();
// mark them all as historical
foreach($documents as $document){
$document->historical = true;
$document->save();
}
return $this->index();
}
public function reAdmitt(User $user)
{
$user->discharged = false;
$user->readmitting = true;
$user->reAdmission_start = now();
$user->save();
return $this->index();
}
public function reAdmitted(User $user)
{
$user->discharged = false;
$user->readmitting = false;
$user->reAdmitted_on = now();
$user->save();
return $this->index();
}
最后是按钮本身
<button><a href="{{route('patients.reAdmitt', $user)}}">Readmitt Patient</a></button>
<button><a href="{{route('patients.reAdmitted', $user)}}">Patient Signed reAdmission</a></button>
<button><a href="{{route('patients.discharge', $user)}}">Discharge Patient</a></button>
预期的结果是它应该 运行 函数并更新数据库,我知道释放函数是有效的,因为我不小心将路由更改为 get 并且它 运行 当页面加载并标记其中一名患者出院。
您没有提交任何表格。所以你不需要使用 post 路线。 <a>
标签用于获取路线。所以把你的路线改成
Route::get('/viewPatient/{user}/discharge', 'PatientController@discharge')->name('patients.discharge');
Route::get('/viewPatient/{user}/reAdmitt', 'PatientController@reAdmitt')->name('patients.reAdmitt');
Route::get('/viewPatient/{user}/reAdmitted', 'PatientController@reAdmitted')->name('patients.reAdmitted')
;
我在获取 blade 文件中的按钮以正确 运行 控制器中的功能时遇到问题,当我单击该按钮时,我得到一个 methodnotallowedexception 并且无法确定找出设置不正确的地方
据我所知,这就像我为不同的 blade 和控制器设置的另一个按钮一样,除非我忽略了一些东西,这就是我在这里的原因。如果您需要其他代码,请告诉我。
首先是我的路线:
Route::post('/viewPatient/{user}/discharge', 'PatientController@discharge')->name('patients.discharge');
Route::post('/viewPatient/{user}/reAdmitt', 'PatientController@reAdmitt')->name('patients.reAdmitt');
Route::post('/viewPatient/{user}/reAdmitted', 'PatientController@reAdmitted')->name('patients.reAdmitted');
接下来是控制器功能
public function discharge(User $user)
{
$user->discharged = true;
$user->discharged_date = now();
$user->current_facility_id = null;
$user->save();
// find all the users documents that are not historical
$documents = Document::where('user_id', $user->id)->where('historical', false)->get();
// mark them all as historical
foreach($documents as $document){
$document->historical = true;
$document->save();
}
return $this->index();
}
public function reAdmitt(User $user)
{
$user->discharged = false;
$user->readmitting = true;
$user->reAdmission_start = now();
$user->save();
return $this->index();
}
public function reAdmitted(User $user)
{
$user->discharged = false;
$user->readmitting = false;
$user->reAdmitted_on = now();
$user->save();
return $this->index();
}
最后是按钮本身
<button><a href="{{route('patients.reAdmitt', $user)}}">Readmitt Patient</a></button>
<button><a href="{{route('patients.reAdmitted', $user)}}">Patient Signed reAdmission</a></button>
<button><a href="{{route('patients.discharge', $user)}}">Discharge Patient</a></button>
预期的结果是它应该 运行 函数并更新数据库,我知道释放函数是有效的,因为我不小心将路由更改为 get 并且它 运行 当页面加载并标记其中一名患者出院。
您没有提交任何表格。所以你不需要使用 post 路线。 <a>
标签用于获取路线。所以把你的路线改成
Route::get('/viewPatient/{user}/discharge', 'PatientController@discharge')->name('patients.discharge');
Route::get('/viewPatient/{user}/reAdmitt', 'PatientController@reAdmitt')->name('patients.reAdmitt');
Route::get('/viewPatient/{user}/reAdmitted', 'PatientController@reAdmitted')->name('patients.reAdmitted')
;