Skip to content

Instantly share code, notes, and snippets.

@hotmeteor
Created August 1, 2018 00:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hotmeteor/d49c1bc4d1377f6a1c8099dd188c8d86 to your computer and use it in GitHub Desktop.
Save hotmeteor/d49c1bc4d1377f6a1c8099dd188c8d86 to your computer and use it in GitHub Desktop.
Database column naming proposal
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateMyTable extends Migration
{
/**
* Run the migrations.
*/
public function up()
{
Schema::create('my_table', function (Blueprint $table) {
// Obvi, increments for `id`
$table->increments('id');
// All foreign keys should be indexed
$table->integer('account_id')->index();
// All columns that *cannot* be empty should *not* be nullable or have an empty default
$table->string('name');
// All columns that *can* be empty should have nullable(), not default('')
$table->json('codes')->nullable();
// All boolean columns should have a default value
// Booleans should always start with `is_` or `has_`
$table->boolean('is_approval_required')->default(false);
// Unit columns should include the unit
$table->integer('job_length_minutes')->nullable();
// Foreign keys to a user should end in `_by`
$table->integer('approved_by')->index()->nullable();
// Timestamps should end in `_at`
$table->timestamp('approved_at')->nullable();
});
}
/**
* Reverse the migrations.
*/
public function down()
{
// Down migrations should exist.
Schema::dropIfExists('my_table');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment