Laravel Project
In this Laravel project tutorial we will learn to create migrations using Artisan.
In the previous tutorial we created a new todo-laravel project. Feel free to check that out.
todos
We will save the todo detail in the todos table.
To create a database table we will take help of make:migration.
make:migration
Open Terminal and run the following command to create a database migration for the todos table.
$ php artisan make:migration create_todos_table --create=todos
The above command will create a new migration file inside the database/migrations directory.
database/migrations
I got the following output when I ran the above command.
Created Migration: 2018_06_19_123348_create_todos_table
And the name of the migration file for me was 2018_06_19_123348_create_todos_table.php.
2018_06_19_123348_create_todos_table.php
create_todos_table
Open the migration file that you created in the above step and you will get to see the following PHP code.
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateTodosTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('todos', function (Blueprint $table) { $table->increments('id'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('todos'); } }
For this project we will have the following columns in the todos table.
The id column is created by the $table->increments('id'); line and it is the primary key.
id
$table->increments('id');
The created_at and updated_at columns are created by the $table->timestamps(); line.
created_at
updated_at
$table->timestamps();
To create the title, description and status columns we will write the following lines.
title
description
status
$table->string('title', 100); $table->text('description'); $table->enum('status', ['ACTIVE', 'DONE', 'DELETED'])->nullable(false)->default('ACTIVE');
Where, column title is of VARCHAR type and of size is 100.
VARCHAR
Column description is of TEXT type. And status column is of ENUM type, NOT NULL and default value is ACTIVE.
TEXT
ENUM
NOT NULL
ACTIVE
Add the three lines inside the up() method and it will look like the following.
up()
public function up() { Schema::create('todos', function (Blueprint $table) { $table->increments('id'); $table->string('title', 100); $table->text('description'); $table->enum('status', ['ACTIVE', 'DONE', 'DELETED'])->nullable(false)->default('ACTIVE'); $table->timestamps(); }); }
Now, we will run the Artisan migrate command to create the tables in the laravel_todo_db database that we created in the Getting Started tutorial.
laravel_todo_db
$ php artisan migrate Migrating: 2014_10_12_000000_create_users_table Migrated: 2014_10_12_000000_create_users_table Migrating: 2014_10_12_100000_create_password_resets_table Migrated: 2014_10_12_100000_create_password_resets_table Migrating: 2018_06_19_123348_create_todos_table Migrated: 2018_06_19_123348_create_todos_table
Note! 2014_10_12_000000_create_users_table and 2014_10_12_100000_create_password_resets_table migration files were created by default by laravel and you can ignore them for now or delete them if your want.
2014_10_12_000000_create_users_table
2014_10_12_100000_create_password_resets_table
If we now check our database laravel_todo_db we will see our todos table and the 6 columns.
You may face the following error when running the php artisan migrate command.
php artisan migrate
Illuminate\Database\QueryException : SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `users` add unique `users_email_unique`(`email`))
To solve this go to app/Providers/AppServiceProvider.php file and add the following piece of code.
app/Providers/AppServiceProvider.php
use Illuminate\Support\Facades\Schema; public function boot() { Schema::defaultStringLength(191); }
Now, rollback and migrate using php artisan migrate:refresh command and hopefully the database migrations will return no error this time.
php artisan migrate:refresh
Alright, we have reached the end of this tutorial. In the next tutorial we will create Model to work with the todos table.
Thanks for reading. Please don't forget to share this tutorial if you find it helpful.
Have fun coding and see you in the next tutorial :)