Recent Learnings: Part 4 Laravel Migrations


2 min read

Laravel Database Migrations

Steps I need to take.

When creating a new laravel project, the first thing to do is create a database.

Open the project in my editor/ide of choice. Open .env to configure the project settings.

I should know what I want to name my database and table.

In order to use a MySQL database I must:

  • Open the project in your editor/ide of choice.

  • Open .env to configure the project settings.
    (while in root directory of project)

  • run commands:
    create database databasename
    In my case i named this practice project nameinfo

  • Now I must create a connection to the database in the IDE.

  • I now need to make a new migration, which I do in artisan.
    A migration is a blueprint for a table I want to have in my new database.
    I want this table to be called create_entries_table. This name is in snakecase and follows a laravel naming convention for tables.
    php artisan make:migration create_entries_table

  • I can open this app in app/database/migrations and select the migration I just made.

  • The migration contains a public function method named up and a where the block of code listed below goes. There is also a down which is for roll backs.`

  • Use of down is contoversial, many developers don't like the mess it can potentially create and take a move forward approach.

  • In the migration, I need to state the names and data types of the columns I want to create. Like this
    Schema::create('nameinfo', function (Blueprint $table) { $table->id(); $table->string('first_name'); $table->string('last_name'); $table->string('fave_color'); $table->timestamps(); });

  • In my controller, I need to provide data for a row in the table.
    I do this inside my index function. or whatever relevant function.
    $data = [

    'first_name' => 'kerry',

    'last_name' => 'owston',

    'fave_color' => 'blue'


  • Now i must ensure there is a model
    php artisan make:model modelname

  • A kind of enforced best practice is now enforced (but it can be bypassed)
    This ensures the right data is mapped to the right column for the migration.

  • public $fillable = [ 'first_name', 'last_name', 'fave_color' ];

  • To bypass for simplicity over security I can also:
    public $guarded = [];

  • Laravel magic knows they are all linked ๐Ÿ˜‰
    I do need to be able to explain that.

  • I'm now ready to migrate the database
    php artisan migrate

  • I can now refresh the database and create the new table with all its columns as I specified.