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
assertDatabaseHasandassertDatabaseMissing.
Related Tips
View all tips →Alternate Test Attributes with Factory sequence() and States
Use sequence() and state methods on Eloquent Factories to alternate attributes and configure complex test fixtures predictably.
Assert Background Job Dispatching with Queue::fake()
Use Queue::fake() to prevent jobs from executing during tests and verify that background tasks are dispatched with correct parameters.