Level 75
You want to use route model binding, so your route have to be like this
Route::delete('/threads/{category}/{thread}/replies/{reply}', 'ReplyController@destroy');
Documentation: https://laravel.com/docs/7.x/routing#route-model-binding
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi everybody,
I'm learning Laravel and I have got the following code to delete a reply. The problem is, my test (in TDD) says I'm still having the DB row, so what am I doing wrong?
My destroy method on the ReplyController
public function destroy($category, $thread, Reply $reply)
{
$reply->delete();
return back();
}
My routes/web.php route
Route::delete('/threads/{category}/{thread}/replies/{id}', 'ReplyController@destroy');
My TDD test: a_user_can_delete_one_of_their_own_replies
/** @test */
public function a_user_can_delete_one_of_their_own_replies()
{
$this->withoutExceptionHandling();
$user = factory('App\User')->create();
$this->actingAs($user);
$reply = factory('App\Reply')->create([
'user_id' => auth()->user()->id
]);
// dd($reply->thread->path().'/replies/'.$reply->id);
$this->delete($reply->thread->path().'/replies/'.$reply->id)
->assertStatus(302);
$this->assertDatabaseMissing('replies', [
'id' => $reply->id
]);
}
My error in the Windows Terminal
There was 1 failure:
1) Tests\Feature\DeleteReplyTest::a_user_can_delete_one_of_their_own_replies
Failed asserting that a row in the table [replies] does not match the attributes {
"id": 1
}.
Found similar results: [
{
"id": "1",
"user_id": "1",
"thread_id": "1",
"body": "Sint facere necessitatibus vel quia vel. Hic sed omnis not iusto suscipit recusandae dolore est. Magni voluptatem voluptate inventore et unde. Non facere nihil enim eligendi dolorem recusandae. Quae eum aut doloremque aliquid quia.",
"created_at": "2020-06-16 19:32:30",
"updated_at": "2020-06-16 19:32:30"
}
].
You want to use route model binding, so your route have to be like this
Route::delete('/threads/{category}/{thread}/replies/{reply}', 'ReplyController@destroy');
Documentation: https://laravel.com/docs/7.x/routing#route-model-binding
Please or to participate in this conversation.