laravel 在 null 上调用成员函数 fill()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35275150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call to a member function fill() on null
提问by Santiago Mu?oz
I want to do an update keeping the following information:
我想做一个更新,保留以下信息:
$radicado = \App\Radicado::find($id);
$radicado->fill($request->all());
$radicado->codigoRadicado = $request['codigoRadicado'];
$radicado->Dependencia_idDependencia = $request['Dependencia_idDependencia'];
$radicado->Serie_idSerie = $request['Serie_idSerie'];
$radicado->SubSerie_idSubSerie = $request['SubSerie_idSubSerie'];
$radicado->save();
if($request->ajax())
{
return response()->json(['message' => 'Actualizado correctamente']);
}
return redirect('/radicado');
and for this run the following ajax
并为此运行以下ajax
var token = document.getElementById('token').value;
$.ajax({
async: true,
headers: {'X-CSRF-TOKEN': token},
url: $(formId).attr('action'),
type: $(formId).attr('method'),
data: $(formId).serialize(),
dataType: 'html',
success: function(result){
$(formId)[0].reset();
alert(result);
document.getElementById("preview").style.display = "none";
},
error: function(){
alert('No se ha actualizado el documento.');
}
});
But when I get this error store: Call to a member function fill() on null
但是当我得到这个错误存储时:Call to a member function fill() on null
采纳答案by patricus
The first two lines shown are this:
显示的前两行是这样的:
$radicado = \App\Radicado::find($id);
$radicado->fill($request->all());
The error is because your find()
statement did not find a record with that $id
. Since it did not find a record, it returns null
. Since it returns null
, $radicado
is now null
, and you cannot call methods on a null
value.
错误是因为您的find()
语句没有找到该记录$id
。由于没有找到记录,它返回null
。由于它返回null
, $radicado
is now null
,并且您不能对null
值调用方法。
回答by Ralph John Galindo
Make sure first that the row exists. Try firstOrFail()
首先确保该行存在。尝试firstOrFail()
$radicado = \App\Radicado::firstOrFail($id);
If its still not working, double check your facade or model.
如果它仍然无法正常工作,请仔细检查您的外观或模型。