Quick Tip: Unit Testing JSON Columns in Laravel

Published on

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

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

Testing Example

1/** @test */
2public method a_form_element_can_be_stored
3{
4 // Do some stuff...
5 
6 $this->assertDatabaseHas('form_elements', [
7 'rules->required' => null,
8 ]);
9}

Check out all our posts on testing with Laravel.