Adding a table and migrate in laravel 5
Here we are creating a table class and then migrating this class to our database with MySql using laravel 5.
Creating table class
C:\wamp\www\laravel>php artisan make:migration author Created Migration: 2015_05_05_103705_author
This will create a class with following code:
<?php use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class Author extends Migration { /** * Run the migrations. * * @return void */ public function up() { // } /** * Reverse the migrations. * * @return void */ public function down() { // } }
Add following code in up method
public function up() { Schema::create('author', function(Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('password', 60); $table->timestamps(); }); }
Add following code in down method
public function down() { Schema::drop('author'); }
Now final file (<some code>author.php)
<?php use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class Author extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('author', function(Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('password', 60); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::drop('author'); } }
Now migrating our class file to a php databse
C:\wamp\www\laravel>php artisan migrate Migrated: 2015_05_05_103705_author
Now your structure will be created as (in MySql database):
CREATE TABLE IF NOT EXISTS `author` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `password` varchar(60) COLLATE utf8_unicode_ci NOT NULL, `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;