Laravel5.4:如何将值从javascript传递给控制器以更新数据
我是拉雷维尔的新手。我想更新一个数据库行。为此,我想将对应行的id从javascript传递给控制器。我可以从视图中获取javascript文件中的id。但在执行此操作时,我遇到了一个错误(PatientController.php第80行中的ErrorException:从空值创建默认对象)。因为控制器没有获取该id。我如何传递该id并解决此问题 我的视图(editPatientView.blade.php): 我的路线:Laravel5.4:如何将值从javascript传递给控制器以更新数据,javascript,php,laravel-5.4,Javascript,Php,Laravel 5.4,我是拉雷维尔的新手。我想更新一个数据库行。为此,我想将对应行的id从javascript传递给控制器。我可以从视图中获取javascript文件中的id。但在执行此操作时,我遇到了一个错误(PatientController.php第80行中的ErrorException:从空值创建默认对象)。因为控制器没有获取该id。我如何传递该id并解决此问题 我的视图(editPatientView.blade.php): 我的路线: Route::post('/savepatientinfo', [
Route::post('/savepatientinfo', [
'uses'=> 'PatientController@SavePatientInfo',
'as'=>'savepatientinfo'
]);
我的控制器():
Laravel使用use LIGHTED\Http\Request;为了处理对HttpRequest不太确定的请求,我假设它是guzzle。尝试导入laravel请求类并尝试dd($request->all());让我们弄清楚您是先获取数据还是先不获取数据是的,我知道,我只是使用illumb\Http\Request作为HttpRequest;这就是全部。我使用dd($request->all());我完全可以得到数据。现在,我能做什么@Rodraneso你收到数据了吗?是的,我收到数据,只是没有从javascript收到相应的id。当我使用dd($patient);,它显示空@Rodrane你到底从哪里得到patientId?为什么不在表单中以隐藏输入的形式发送它,看起来好像您没有发布ajax请求的所有内容?
var patientId = 0;
$('#edit').on('click', function (event) {
patientId = event.target.parentNode.parentNode.dataset['patientid'];
alert(patientId);
$.ajax({
method: 'POST',
url : urlEdit,
data: {
patientId: patientId,
_token: token
}
});
});
Route::post('/savepatientinfo', [
'uses'=> 'PatientController@SavePatientInfo',
'as'=>'savepatientinfo'
]);
public function SavePatientInfo(HttpRequest $request)
{
// validation...
$this->validate($request, [
'patient_name'=> 'required|max:2000',
'patient_age'=> 'required',
'symptom'=> 'required',
]);
$patient = Patient::find($request['patientId']) ;
// dd($patient);
$user = Auth::user();
$patient->name = $request['patient_name'];
$patient->age = $request['patient_age'];
$patient->weight = $request['patient_weight'];
$patient->symptom = $request['symptom'];
$patient->test = $request['test'];
$patient->return_date = $request['return-date'];
$patient->user()->associate($user);
$patient->update();
if($request->user()->patients()->save($patient))
{
$message = 'Patient information enrolled successfully' ;
}
return redirect()->route('showpatientinfo', ['user_id'=> $patient->user->id, 'patient_id'=>$patient->id])->with(['message'=>$message]);
}