Skip to content

Instantly share code, notes, and snippets.

@kbond
Last active November 21, 2018 20:50
Show Gist options
  • Save kbond/31ffc2463393acf1043a43233af96860 to your computer and use it in GitHub Desktop.
Save kbond/31ffc2463393acf1043a43233af96860 to your computer and use it in GitHub Desktop.
Laravel advanced migration examples
<?php
Schema::create('visits', function (Blueprint $table) {
$table->increments('id');
$table->string('url');
$table->json('data');
// non-nullable timestamps
$table->timestamp('created_at')->default(\DB::raw('CURRENT_TIMESTAMP'));
$table->timestamp('updated_at')->default(\DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));
// generated virtual column from json with proper null casting
$table->string('meta_country')->virtualAs("CASE WHEN data->>'$.country' = 'null' THEN NULL ELSE data->>'$.country' END");
$table->index('meta_country');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment