Skip to content

Instantly share code, notes, and snippets.

@jasonlbeggs
Created March 6, 2021 00:49
Show Gist options
  • Save jasonlbeggs/878c44c78750a359f297bc91e28f58e4 to your computer and use it in GitHub Desktop.
Save jasonlbeggs/878c44c78750a359f297bc91e28f58e4 to your computer and use it in GitHub Desktop.

Testing JSON Columns

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,
    ]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment