Creating a database and migrating the images table

After successfully installing Laravel and defining database credentials from config/database.php, create a database called images. For this, you can either create a new database from your hosting provider’s panel, or if you are the server administrator, you can simply run the following SQL command:

CREATE DATABASE images

After successfully creating the database for the application, we need to create a images table and install it to the database. To do this, open up your terminal, navigate to your project folder and run the following command:

php artisan make:migration create_images_table --table=images –create

This command will generate a new MySQL database migration for us to create a table named images.

Now we need to define what sections should be in our database table. For our example, I thought id column, image titles, image file names, and timestamps should be sufficient. So for this, open the migration file just created with the preceding command and change its contents as shown in the following code:

<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateimagesTable extends Migration {

  /**
  * Run the migrations.
  * @return void
  */
  public function up()
    {
    Schema::create('images', function(Blueprint $table)
    {
      $table->increments('id');
      $table->string('title',400)->default('');//the column that holds the image's name
      $table->string('image',400)->default('');//the column that holds the image's filename
      $table->timestamps();
    });
  }

  /**
  * Reverse the migrations.
  * @return void
  */
  public function down()
  {
    Schema::drop('images');
  }
}

After saving the file, run the following command to execute migrations:

php artsian migrate

If no error has occurred, you are ready for the next step of the project.

Chapter 2 of 9