Santiago Muñoz February 2016

Call to a member function fill() on null

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

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

Answers


patricus February 2016

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.


Ralph John Galindo February 2016

Make sure first that the row exists. Try firstOrFail()

$radicado = \App\Radicado::firstOrFail($id);

If its still not working, double check your facade or model.

Post Status

Asked in February 2016
Viewed 3,331 times
Voted 12
Answered 2 times

Search




Leave an answer