Migration and Schema Builder in Laravel

Laravel provides Migration and Schema classes for manipulating database tables. It is independent of database type. Migration is useful in version control. For creating a migration, first give command:

  1. php artisian make:migration table_name

It will create a new file in folder database >> migrations with name year_month_day_seconds_table_name.php. For example, above command will create file with name something like 2014_10_12_000000_table_name.php. The default code of this file is:

  1. <?php
  2. use Illuminate\Database\Schema\Blueprint;
  3. use Illuminate\Database\Migrations\Migration;
  4. class TableName extends Migration {
  5. /**
  6. * Run the migrations.
  7. *
  8. * @return void
  9. */
  10. public function up()
  11. {
  12. //
  13. }
  14. /**
  15. * Reverse the migrations.
  16. *
  17. * @return void
  18. */
  19. public function down()
  20. {
  21. //
  22. }
  23. }

The Blueprint class provides SQL for creating columns in tables. TableName class has two functions, up() and down(). The code for manipulating the table is written in the up() function. The down() function is used to undo the work of up() function. For example if up() function contains the code for creating a table then down() function will take the code for deleting that table.

Creating and deleting table:

Schema::create() and Schema::drop() are used for creating and deleting table respectively. In database >> migrations folder, there is a default file named 2014_10_12_000000_create_users_table.php. The code inside this file is:

  1. <?php
  2. use Illuminate\Database\Schema\Blueprint;
  3. use Illuminate\Database\Migrations\Migration;
  4. class CreateUsersTable extends Migration {
  5. /**
  6. * Run the migrations.
  7. *
  8. * @return void
  9. */
  10. public function up()
  11. {
  12. Schema::create('users', function(Blueprint $table)
  13. {
  14. $table->increments('id');
  15. $table->string('name');
  16. $table->string('email')->unique();
  17. $table->string('password', 60);
  18. $table->rememberToken();
  19. $table->timestamps();
  20. });
  21. }
  22. /**
  23. * Reverse the migrations.
  24. *
  25. * @return void
  26. */
  27. public function down()
  28. {
  29. Schema::drop('users');
  30. }
  31. }

The create() function takes two arguments: table name and operations on the columns of that table (it is a closure). The drop() function takes only table name as argument. In above code, we are creating a table users in up function and deleting this table in down function.

Operations on columns:

Add:

Syntax for adding column is:

  1. $table->column_type('column_name')->more_operation();

List of all column types is here: Schema Builder in Laravel.

Change:

For changing column use change() function. For example, for changing name column (size 20 to 50) use:

  1. $table->string('name', 50)->change();

Rename:

For rename use renameColumn() function.

  1. $table->renameColumn('from', 'to');

Delete:

For deleting a column use dropColumn() function like

  1. $table->dropColumn('votes');

Adding and dropping Indexes:

For adding indexes use indexes functions like:

  1. <span id=" selection-marker-1"="">$table->string('email')->unique();

Similarly for dropping indexes use crossponding functions like

  1. $table->dropUnique('users_email_unique');

Adding Foreign Key:

For adding a foreign key, first define the column as unsigned type and then add reference (foreign column) and foreign table with it.

  1. $table->integer('user_id')->unsigned();
  2. $table->foreign('user_id')->references('id')->on('users');

Here we are referencing user_id column to id column of users table.

Also see Django vs Laravel vs Ruby on rails

Checking Existence:

For checking existence of a table/ column use Schema::hasTable('table_name') / Schema::hasColumn('table_name', 'column_name').

Dropping Timestamps & SoftDeletes:

For deleting timestamps and softdeletes use functions $table->dropTimestamps(); and $table->dropSoftDeletes(); respectively.

Migration commands:

  1. php artisan migrate: Update database. If you will run this command after configuring database settings then laravel will automatically create/ modify the database tables according to migrations defined in migrations folder. By default laravel will form three tables (users, password_reset and migrations). migrations table in database handles the migrations (store migration entries defined in migration folder).
  2. php artisan migrate:rollback: Undo last migration.
  3. php artisan migrate:reset: Undo all migrations. It will delete all tables from database except migrations table.
  4. php artisan migrate:refresh: Undo all migrations and then migrate again.

More Tutorials on laravel in this series:



About Harish Kumar

Harish is an interested person in the field of web development and blogging. He works for the need of young web developers in learning various languages, latest technologies and other essential tips and tricks. If you need some help or you have some suggestion then you email him at harish@findalltogether.com without any hesitation. You can also suggest/demand for articles of your own choice.

Related Articles

Laravel is a MVC framework. Models are the real world entities in data form. Views are the response content to the vi...
Laravel framework architecture
Routes map urls with the controllers. In this section we will read about how route maps urls with controllers and how...
Basics routing and controllers in laravel 5
Views are the HTML content served by the server. These are served by returning from controllers. Views are .php or .b...
View in Laravel 5

Login or Sign up to leave comment.


1 Comments

  • Ivan
    I think there's a tipfeller in "php artisian make:migration table_name". It should be artisan.
    January 2, 2016 at 11:01 AM Reply

    Login or Sign up to reply.