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.
Aug 28, 2023
5
Level 1
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.
Please or to participate in this conversation.