Laravel Testing

Verify Model Persistence with assertModelExists() and assertModelMissing()

Punyapal Shah 1 min read
edit this tip
Replace raw assertDatabaseHas('table', ['id' => $model->id]) calls with model-aware assertModelExists() and assertModelMissing() assertions.

When writing unit and feature tests in PHPUnit or Pest PHP, checking whether an Eloquent model exists or was deleted from the database traditionally required specifying the table name and ID explicitly.

Laravel provides model-aware persistence assertions.

Testing Model Existence

use App\Models\User;

test('user can be created and deleted', function () {
    $user = User::factory()->create();

    // Model-aware existence assertion
    $this->assertModelExists($user);

    // Delete user
    $user->delete();

    // Verify model was removed from database
    $this->assertModelMissing($user);
});

In Pest PHP Expectation Syntax

use App\Models\Post;

it('creates post successfully', function () {
    $post = Post::factory()->create();

    expect($post)->toBeInstanceOf(Post::class);
    $this->assertModelExists($post);
});

Summary

  • Inspects the model's table name and primary key automatically.
  • Prevents table-renaming breakage in test suites.
  • Cleanly complements assertDatabaseHas and assertDatabaseMissing.
Tags: Laravel Testing Eloquent Pest PHP
Share on X

// Found an issue or want to contribute a tip? github.com/MrPunyapal/tips