我在更新数据库中的行时遇到了问题。一旦我提交更新表单,它就会返回并发生错误Call to a member function update() on null
有人可以告诉我这里我做错了吗?
我的代码:
public function update(Request $request){
$user = Auth::user();
$uid = $user->farmTag;
$cowValues = array(
'KilledOn' => $request['KilledOn'],
'EID' => $request['EID'],
'ManagementI/D' => $request['ManagementID'],
'name' => $request['name'],
'breed' => $request['breed'],
'sex' => $request['sex'],
'DOB' => $request['DOB'],
'BoughtOn' => $request['BoughtOn'],
'description' => $request['description'],
'farmTag' => $uid,
);
Cow::find($request['EID'])->update($cowValues);
return back();
}
完整错误:
in CowsController.php line 280
at CowsController->update(object(Request))
at call_user_func_array(array(object(CowsController), 'update'), array(object(Request))) in compiled.php line 9355
at Controller->callAction('update', array(object(Request))) in compiled.php line 9382
at ControllerDispatcher->dispatch(object(Route), object(CowsController), 'update') in compiled.php line 8440
at Route->runController() in compiled.php line 8421
at Route->run(object(Request)) in compiled.php line 8117
at Router->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in compiled.php line 9948
at SubstituteBindings->handle(object(Request), object(Closure)) in compiled.php line 9840
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Authenticate.php line 43
at Authenticate->handle(object(Request), object(Closure)) in compiled.php line 9840
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in compiled.php line 3121
at VerifyCsrfToken->handle(object(Request), object(Closure)) in compiled.php line 9840
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in compiled.php line 13430
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in compiled.php line 9840
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in compiled.php line 11874
at StartSession->handle(object(Request), object(Closure)) in compiled.php line 9840
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in compiled.php line 13176
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in compiled.php line 9840
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in compiled.php line 13113
at EncryptCookies->handle(object(Request), object(Closure)) in compiled.php line 9840
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in compiled.php line 9825
at Pipeline->then(object(Closure)) in compiled.php line 8118
at Router->runRouteWithinStack(object(Route), object(Request)) in compiled.php line 8109
at Router->dispatchToRoute(object(Request)) in compiled.php line 8100
at Router->dispatch(object(Request)) in compiled.php line 2446
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in compiled.php line 3183
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in compiled.php line 9840
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in compiled.php line 9825
at Pipeline->then(object(Closure)) in compiled.php line 2390
at Kernel->sendRequestThroughRouter(object(Request)) in compiled.php line 2374
at Kernel->handle(object(Request)) in index.php line 53
如果缺少某些信息,请告知我们。
谢谢!
答案 0 :(得分:0)
试试这个
$cow = Cow::where('EID', $request->input('EID'))->first();
if($cow)
{
$cow->update($cowValues);
}