How to Create Model with Migration Script in Laravel

Posted on

This article is written to specify the way to achieve on how to create model with migration script in Laravel web-based application project. The important thing which is focused on the above title is the urge of creating a migration script in Laravel web-based application project as soon as a model successfully created.

A migration script created will be convenient so that the generation of a table associated to the model will be automatically executed. As long as the structure for the table is specified within the model file, the structure defined inside the model file will become a guideline or a pattern used to create table associated with the model. Below is an example for creating a model along with the migration script as well :

php artisan make:model ModelName -m

For an example :

user@hostname:/var/www/html/laravel$ php artisan make:model Service -m 
Model created successfully.
Created Migration: 2017_11_03_060734_create_services_table
user@hostname:/var/www/html/laravel$

Based on the above output, not only a model named Service successfully created but also a migration script file named 2017_11_03_060734_create_services_table. Normally, a model will be generated and it will be stored in a folder named ‘app/’. The other file which is a migration script file will be generated and it will be stored in a folder named migrated located in ‘database/migrations’ from the root folder of Laravel web-based application project as shown below :

user@hostname:/var/www/html/laravel/database/migrations$ ls -al
total 40
drwxrwxr-x 2 www-data user  4096 Nov 3 13:07 .
drwxrwxr-x 5 www-data user  4096 Aug 30 16:55 ..
-rw-rw-r-- 1 www-data user  746  Aug 30 16:55 2014_10_12_000000_create_users_table.php
-rw-rw-r-- 1 www-data user  683  Aug 30 16:55 2014_10_12_100000_create_password_resets_table.php
-rw-rw-r-- 1 www-data user  592  Nov 2 22:05 2017_11_02_150552_create_tickets_table.php
-rw-rw-r-- 1 www-data user  601  Nov 2 22:06 2017_11_02_150646_create_categories_table.php
-rw-rw-r-- 1 www-data user  601  Nov 2 22:12 2017_11_02_151257_create_categories_table.php
-rw-rw-r-- 1 www-data user  601  Nov 2 22:19 2017_11_02_151921_create_categories_table.php
-rw-r--r-- 1 www-data user  595  Nov 2 22:41 2017_11_02_154132_create_comments_table.php
-rw-rw-r-- 1 usre     user  595  Nov 3 13:07 2017_11_03_060734_create_services_table.php
user@hostname:/var/www/html/laravel/database/migrations$

There are a migration script file created named 2017_11_03_060734_create_services_table.php in the last line as shown in the above output. Below is the content of the file :

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateServicesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('services', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
}

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

Below is the content of the model created :

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Service extends Model
{
//
}

The schema depicting the structure of the table can be defined in the model class file.

Supposed we are going to add an additional column named ‘name’ after a column which has the type of increment in integer. The thing which can be done is editing the migration script file and add the following line :

$table->increments('id');

Just add that line into the following part so it will be shon as follows :

Schema::create('services', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();

After editing the migration script file, execute it by running the following command :

php artisan migrate

The output of the above command executed in a command line can be shown as follows :

user@hostname:/var/www/html/laravel$ php artisan migrate
Migrating: 2017_11_03_060734_create_services_table
Migrated: 2017_11_03_060734_create_services_table
user@hostname:/var/www/html/laravel$

The impact is in the database generated, there will be an additional table named ‘services’. It can be shown as follows :

mysql> clear
mysql> show tables;
+--------------------------+
| Tables_in_laravel_db     |
+--------------------------+
| categories               |
| comments                 |
| migrations               |
| password_resets          |
| services                 |
| tickets                  |
| users                    |
+--------------------------+
7 rows in set (0,00 sec)

mysql> desc services;
+------------+------------------+------+-----+---------+--------------+
| Field      | Type             | Null | Key | Default | Extra        |
+------------+------------------+------+-----+---------+--------------+
| id         | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name       | varchar(255)     | NO   |     | NULL    |                |
| created_at | timestamp        | YES  |     | NULL    |                |
| updated_at | timestamp        | YES  |     | NULL    |                |
+------------+------------------+------+-----+---------+--------------+
4 rows in set (0,01 sec)

mysql>

One thought on “How to Create Model with Migration Script in Laravel

Leave a Reply