Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MohamedLamineAllal/efee3894f9c4cda97662d34f1492b068 to your computer and use it in GitHub Desktop.
Save MohamedLamineAllal/efee3894f9c4cda97662d34f1492b068 to your computer and use it in GitHub Desktop.
Laravel migrations boilerplate
<?php
// fields types
$table->increments('id');
//integer and unsigned (needed when creating foreign keys
$table->integer('company_id')->unsigned();
$table->string('name')->nullable(false);
$table->string('slug')->nullable(false);
$table->string('unique_code')->nullable(false);
//string varchar with a size
$table->string('unit', 45)->nullable(false);
//datetime
$table->datetime('start_period')->nullable(false);
$table->datetime('end_period')->nullable(false);
$table->double('monthly_price')->nullable(true);
$table->integer('consumption_details_id')->unsigned();
//unique too
$table->string('name')->nullable(false)->unique();
// default value
$table->double('current_payed_amount')->nullable(false)->default('0'); // note you need to pass a string ('0' not 0)
// adding create_at updated_at (timestamps automatically)
$table->timestamps();
//add a comment
$table->string('is_default')->nullable(false)->comment = 'to specify if this should be the default plan';
$table->string('is_default')->nullable(false)->default('0')->comment = 'to specify if this should be the default plan';
//primary key
$table->primary('id'); //Adds a primary key.
$table->primary(['id', 'parent_id']); //for multiple (composite pk)
// unique contstraint
//--------one
$table->string('email')->unique()
//--------multiple
$table->unique(['name', 'module', 'variant']);
//Schema class
//To create a table
Schema::create('pricing_plans', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->nullable(false)->unique();
$table->string('description')->nullable(true);
$table->string('is_default')->nullable(false);
$table->string('note')->nullable(true);
$table->timestamps();
});
//modify alter update a table
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePricingPlansTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('pricing_plans', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->nullable(false)->unique();
$table->string('description')->nullable(true);
$table->string('is_default')->nullable(false);
$table->string('note')->nullable(true);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('pricing_plans');
}
}
//With config (db name foreign key ...etc)
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePricingListTable extends Migration
{
public function __construct () {
$this->tname = config('migrations.pricing_list.tname');
}
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create($this->tname, function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists($this->tname);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment