Skip to content

Instantly share code, notes, and snippets.

@Jmrich
Last active January 2, 2019 15:04
Show Gist options
  • Save Jmrich/752adaea13645ba496fa5b8110962261 to your computer and use it in GitHub Desktop.
Save Jmrich/752adaea13645ba496fa5b8110962261 to your computer and use it in GitHub Desktop.
Assert database has json values
// user class
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
protected $casts = [
'settings' => 'array'
];
}
// migration
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->json('settings')->nullable();
$table->rememberToken();
$table->timestamps();
});
}
// test
public function testCanVerifyJsonColumn()
{
$expectedData = [
'profile_picture' => 'profile picture',
];
$user = factory(User::class)->create([
'settings' => $expectedData
]);
$this->assertDatabaseHas('users', [
'settings' => json_encode($expectedData),
]);
}
@mostafabahri
Copy link

For Postgres json fails but jsonb is okay.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment