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

serpapi logo
SerpApi

The Web Search API for Your LLM and AI Applications

Visit SerpApi

The latest

View all →
Image Dominant Color and HEIC Support in Laravel 13.24 image

Image Dominant Color and HEIC Support in Laravel 13.24

Read article
Official Laravel Zed Extension: LSP for PHP & Blade image

Official Laravel Zed Extension: LSP for PHP & Blade

Read article
Laravel Head: Manage Meta Tags, Open Graph, and JSON-LD image

Laravel Head: Manage Meta Tags, Open Graph, and JSON-LD

Read article
PhpStorm 2026.2 Released image

PhpStorm 2026.2 Released

Read article
Laravel Doctor: Diagnose Your App With One Artisan Command image

Laravel Doctor: Diagnose Your App With One Artisan Command

Read article
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article