Database Conventions

Table and Fields Naming

Table names MUST be in plural form and MUST be all lower-case

Good

class CreateFlightsTable extends Migration

{

public function up()

{

Schema::create('flights', function (Blueprint $table) {

Bad

class CreateFlightsTable extends Migration

{

public function up()

{

Schema::create('flight', function (Blueprint $table) {

class CreateUsersTable extends Migration

{

public function up()

{

Schema::create('MyUsers', function (Blueprint $table) {

Pivot table names MUST be in singular model names in alphabetical order

Good

post_user

article_user

photo_post

Bad

posts_users

user_articles

post_photos

Table column names SHOULD be in snake_case without the model name

Good

username

title

thumb_url

Bad

UserName

_title

ThumbUrl

post_title

Foreign keys MUST be singular model name with _id suffix

Good

user_id

Bad

userid

siteid

Memberid

TransactionID

Primary Keys SHOULD be “id”

Good

id

Bad

ID

pkid

guid

Database Alterations

You MUST not be changing the database schema directly, use Database Migrations instead

Good

php artisan migrate

Bad

  • use of PHPMyAdmin

  • directly executing ALTER statement in mysql console / cli

  • using sql file to change the db

Migration filenames MUST follow to following pattern

creation of table

yyyy_mm_dd_<timestamp>_create_<table name>_table

Good

2019_06_06_164210_create_domains_table.php

Bad

2019_06_06_164210_domains.php

Database Choice

Polyglot Persistence

Is a practice of using different data storage technologies for different kinds of data. Eloquent ORM can support multiple database for a reason, so don’t limit yourself to MySQL.

  • It is RECOMMENDED to use MongoDB for records that have attributes that vary a lot. For example, in an inventory system, an office supplies product might have a different set of fields compared to vehicle and auto supplies.

  • It is RECOMMENDED to use ElasticSearch for high volume data searching and indexing.

  • It is RECOMMENDED to use Neo4J for applications that require complex relationships between models. For example a multi-level networking application, social network site and similar apps.

From this article, here is a sample breakdown of different databases being used by a retailer company

Last updated