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

surgawa's avatar

Error: Call to a member function delete() on null

I'm encountering an issue in my Laravel application and I could use some assistance in resolving it. I'm attempting to delete a record using a form in Laravel, and I keep getting the "Call to a member function delete() on null" error.

Here's how my code is structured:

Route Definition:

Route::delete('/hapus-persalinan/{id}', [PersalinanController::class, 'hapusPersalinan'])->name('hapusPersalinan');

Controller Method:

public function hapusPersalinan($id)
{
    $persalinan = Persalinan::find($id);

    $persalinan->delete();

    return redirect()->back();
}

View Code:

<form method="POST" action="{{ route('hapusPersalinan', $id) }}" style="display: inline-block;">
                                @csrf
                                @method('DELETE')
                                <button type="submit" class="btn btn-secondary">
                                    <i class="fa fa-trash"></i>
                                </button>
                            </form>

I expect the record to be deleted upon submitting the form, but instead, I'm encountering the "Call to a member function delete() on null" error.

Any insights or suggestions would be greatly appreciated.

0 likes
5 replies
JussiMannisto's avatar

The Persalinan model with that id doesn't exist. When you call Persalinan::find($id), you get null. When you try to call delete() on that, you get this error.

surgawa's avatar

@JussiMannisto Could the relationship between the Persalinan and Ibu tables be contributing to this error? Since there is a relationship between these tables

JussiMannisto's avatar

@surgawa You should call this after retrieving the model, before calling delete:

dd($persalinan);

If it shows null, then that's the issue. But you should really share the full error message so that we can see where the error is.

If it is null, you should check the id parameter that you're passing to the route. Make sure it's an id of the correct model.

surgawa's avatar

@JussiMannisto Yes, after following your suggestion, I added dd($persalinan) and it indeed returns null. As for the error message, it appears as follows:

Call to a member function delete() on null

This error is specifically associated with the line of code:

 $persalinan->delete();
JussiMannisto's avatar

@surgawa Then the Persalinan model with that id doesn't exist. Check what id you're passing to the route.

Please or to participate in this conversation.