Laravel Tutorials

Quick Tip: Unit Testing JSON Columns in Laravel

Published
Quick Tip: Unit Testing JSON Columns in Laravel image

You may know that you can query against JSON columns using the column->key syntax in Laravel, but I recently learned you can use the same syntax in your tests when asserting that the database has certain data!

Query Example

FormElement::where('rules->required', null)->get();

Testing Example

/** @test */
public method a_form_element_can_be_stored
{
// Do some stuff...
 
$this->assertDatabaseHas('form_elements', [
'rules->required' => null,
]);
}

Check out all our posts on testing with Laravel.

Jason Beggs photo

TALL stack (Tailwind CSS, Alpine.js, Laravel, and Livewire) consultant and owner of designtotailwind.com.

Sponsored

masteringlaravel logo
Laravel Code Review

Get expert guidance in a few days with a Laravel code review

Visit Laravel Code Review

The latest

View all →
Mercure Broadcasting in Laravel 13.32 image

Mercure Broadcasting in Laravel 13.32

Read article
Super Stack: Laravel Starter Kit With Filament and NativePHP image

Super Stack: Laravel Starter Kit With Filament and NativePHP

Read article
Laravel MCP 1.0 Is Released image

Laravel MCP 1.0 Is Released

Read article
Laravel Vet: Review Composer Code Before It Installs image

Laravel Vet: Review Composer Code Before It Installs

Read article
What's New in PHP 8.6 image

What's New in PHP 8.6

Read article
Building EasyReply: How We Used Laravel to Unify Customer Support image

Building EasyReply: How We Used Laravel to Unify Customer Support

Read article