Todo Laravel Project - Migration - Creating todos table

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.

The todos table

We will save the todo detail in the todos table.

To create a database table we will take help of 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.

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.

The create_todos_table migration file

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');
  }
}

Adding columns to the todos table

For this project we will have the following columns in the todos table.

  • id
  • title
  • description
  • status
  • created_at
  • updated_at

The id column is created by the $table->increments('id'); line and it is the primary key.

The created_at and updated_at columns are created by the $table->timestamps(); line.

To create the title, description and status columns we will write the following lines.

$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.

Column description is of TEXT type. And status column is of ENUM type, NOT NULL and default value is ACTIVE.

Add the three lines inside the up() method and it will look like the following.

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();
  });
}

php artisan migrate

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.

$ 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.

If we now check our database laravel_todo_db we will see our todos table and the 6 columns.

Possible error

You may face the following error when running the php artisan migrate command.

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.

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.

Next tutorial: Model

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 :)

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT