Last active
November 21, 2018 20:50
-
-
Save kbond/31ffc2463393acf1043a43233af96860 to your computer and use it in GitHub Desktop.
Laravel advanced migration examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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