I don't trust my memory well so I'll document some migrations command
php artisan make:migration add_paid_to_users --table="users"
This line will create a book table migrations
php artisan make:migration create_books_table --create="books"
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateBooksTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('books', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('books');
}
}
This line will add a migration script to add author_id
php artisan make:migration add_author_id_to_book_table --table="books"
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddAuthorIdToBookTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('books', function (Blueprint $table) {
$table->integer('author_id');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('books', function (Blueprint $table) {
//
});
}
}
My Template for creating migrations
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades;
class DifferentArtisanTypeForMySql extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('my_table1', function (Blueprint $table) {
$table->increments('id');
$table->string('string_col');
$table->string('string_col_with_length', 500);
//create unsigned field which is the same type for field with increment attribute
$table->unsignedInteger('unsigned_int_index')->index();
//create string token with unique index
$table->string('token', 100)->unique();
$table->enum('enum_field', ['Beauty', 'Business service', 'Childrens products'])->nullable();
$table->timestamp('timestamp_field')->nullable();
$table->date('date_field')->nullable();
$table->boolean('boolean_field')->default(0);
//will create integer field with 11 in length
$table->integer('integer_field')->default(0);
//will create integer field with 10 in length
$table->integer('unsigned_integer_field')->unsigned();
$table->decimal('decimal_field', 6, 1)->nullable()->default(0);
$table->tinyInteger('tinyint_field');
$table->smallInteger('smallinteger_field')->nullable();
$table->char('char_field', 2)->default('');
$table->text('text_field');
});
// DB::statement('ALTER TABLE campaign_influencer_posts CHANGE COLUMN social_id account_id INT(11) NOT NULL ;');
// Create table non auto increment field with index field
Schema::create('my_table2', function ($table) {
$table->integer('id')->unsigned()->index();
$table->string('capital', 255)->nullable();
$table->string('citizenship', 255)->nullable();
$table->primary('id');
});
//Do you want to add a new records right away?
Schema::create('my_table3', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->tinyInteger('active')->index();
});
DB::table('my_table3')->insert([
'name' => 'Instagram',
'active' => 1
]);
DB::table('my_table3')->insert([
'name' => 'Facebook',
'active' => 1
]);
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('my_table1', function (Blueprint $table) {
$table->dropColumn('string_col');
});
// DB::statement('ALTER TABLE campaign_influencer_posts CHANGE COLUMN account_id social_id INT(11) NOT NULL ;');
Schema::dropIfExists('my_table1');
Schema::dropIfExists('my_table2');
Schema::dropIfExists('my_table3');
}
//You might wish to rename some fields
// but for laravel 5.X
// you need to run composer require doctrine/dbal
public function up()
{
Schema::table('demographics', function (Blueprint $table) {
$table->renameColumn('social_id', 'account_id');
});
}
}
Template for migration with foreign key
public function up(): void
{
Schema::table('child_table', function (Blueprint $table) {
$table->integer('foreign_column', false, true)->nullable()->after('column_name');
$table->foreign('foreign_column')->references('id')->on('parent_table');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('child_table', function (Blueprint $table) {
$table->dropForeign('foreign_key_here');
$table->dropColumn('foreign_column');
});
}
Here are the list of data type columns that can be added https://laravel.com/docs/5.5/migrations#creating-columns