In Laravel and Pest tests, assertDatabaseHas() natively queries nested JSON properties using arrow syntax and accepts backed Enum instances directly.
Testing JSON attributes or PHP backed enums in database assertions requires no manual casting, JSON encoding, or ->value calls.
assertDatabaseHas() natively serializes backed enums and translates arrow notation into database JSON queries.
Code Examples
use App\Enums\UserRole;
// In Pest tests:
assertDatabaseHas('users', [
'role' => UserRole::Maintainer, // Serializes backed enum automatically
'settings->theme' => 'dark', // Queries nested JSON key
'settings->notifications->email' => true,
]);
// In PHPUnit tests:
$this->assertDatabaseHas('users', [
'role' => UserRole::Maintainer,
'settings->theme' => 'dark',
]);
Key Benefits
- Enum Serialization: Passes backed enum instances directly without manual
->valueextraction. - Nested JSON Traversal: Arrow syntax (
settings->theme) queries database JSON paths across MySQL, PostgreSQL, and SQLite. - Framework Agnostic: Works identically in both Pest test functions and PHPUnit test cases.
Related Tips
View all tips →Process Improvements in Laravel 13.26: Idle Timeouts and Testing Assertions
Laravel 13.26 introduces ProcessIdleTimedOutException to distinguish idle processes from overall runtime timeouts, alongside new testing assertions for execution count and sequence order.
Use Immutable Dates by Default in Laravel
Configure Laravel's Date facade to use CarbonImmutable by default, and automate migrating legacy Carbon calls with Rector.