Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

surgawa's avatar

Getting 'Call to a member function delete() on null' error when deleting record

Hello, im in the early stage of learning, so please enlightened me.

I'm encountering an issue while attempting to delete a record in my Laravel application. The problem is leading to the following error message: "Call to a member function delete() on null"

In my controller, I have a method intended to delete a record associated with a specific "patient" ID. The method looks like this:

delete controller

public function deleteAntenatal($id)
{
    Antenatal::find($id)->delete();
    return redirect()->route('listAntenatal');
}

view

<a href="{{ route('deleteAntenatal', $anc->id) }}" class="btn btn-danger">
             <i><i class="fa fa-trash"></i></i>
</a>

While attempting to delete the record with a specific "patient" ID, I'm encountering the "Call to a member function delete() on null" error. Could the presence of the related "patient" ID be causing this issue? If so, how can I address this error and successfully delete the associated record?

0 likes
7 replies
Tray2's avatar

You shouldn't use a link to delete records, you need to use a form and do a post request. Anyway The id you are passing into your method doesn't exist in the table in the database, that is why you get that kind of error.

Snapey's avatar

browsers can pre-fetch links so maybe your browser has already deleted it for you.

One good reason why things that change server status (such as deletes!) should be POSTed forms.

surgawa's avatar

@tray2 @snapey Thank you for your suggestion regarding using a form for record deletion. I followed your advice and updated my code to use a form with a POST request. However, I'm still encountering the 'Call to a member function delete() on null' error when attempting to delete a record.

Also thanks for bringing up the possibility of link pre-fetching. I've double-checked the data in the database, and the record I'm attempting to delete still exists. I've verified this by confirming the record's presence both before and after attempting the deletion.

Snapey's avatar

@surgawa Then you are not passing the ID of the record correctly to the route.

surgawa's avatar

@Snapey To provide more context, here's the relevant portion of my code:

<form method="POST" action="{{ route('deleteAntenatal', $anc->id) }}" style="display: inline-block;">
    @csrf
    @method('DELETE')

    <button type="submit" class="btn btn-danger" onclick="return confirm('Are you sure you want to delete this record?')">
        <i class="fa fa-trash"></i>
    </button>
</form>

The route configuration is as follows:

Route::prefix('antenatal')->group(function () {
    Route::get('/delete-antenatal/{id}', [AntenatalController::class, 'deleteAntenatal'])->name('deleteAntenatal');
});

Based on this setup, is there anything specific I should double-check or any other potential reasons why the ID might not be passed correctly to the route?"

Please or to participate in this conversation.