How to Create a Table Using Migration in Laravel?

Last updated on February 28, 2023

In Laravel, the migration feature creates and edits tables. It has many functions that return code to create or edit tables.  Any kind of database structure can be developed by using the migration feature.

Create Migration File

Laravel has a migration command that creates migration. Laravel keeps migrations into the migrations folder which location in the database folder. Following is the command with the correct format of the table name and an option that tells to Laravel create a new table.

php artisan make:migration create_table_name_table -–create=table_name

It is necessary to keep the table name plural. So for example, if I need to create a table for cups then the table name should look like the below in the command,

php artisan make:migration create_cups_table -–create=cups

Now, this command would add a file inside the migration folder with the following class,

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

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

In the migration class, there are two methods up() and down(). The migration feature runs the first method up() when it gets the following command,

php artisan migrate

It runs the second method down() when it gets the following command,

php artisan migrate:rollback

Laravel migration feature takes the name from command and creates a class with the same name by following the upper camel case naming convention. Also, it uses the same name for the file along with a current timestamp. Following is the name format,

Year_Month_Day_CurrentTime

I got the following file name after running the above command for the table of the cup,

2022_06_07_171605_create_cups_table.php

Add Columns to Migration

Next, we need to add columns to the table of the cups. Following are columns that need to be added to the table of the cups,

  • id
  • name
  • description
  • image
  • price
  • is_active
  • created_at
  • updated_at

Column id:

There is no need to add an id column because Laravel migration adds it by default.

Column name:

The name does not contain long text so we can use the string() function that would add varchar data type. Function string() accepts two parameters name and length, the first one is required and the second is optional. By default string() function put 255 length for the column.

Column description:

Usually, we add long text in the description column. Laravel provides text() function which adds text data type.

Conclusion

For creating a table in the Laravel framework, create a migration file through php artisan make:migration command and add the code for the required columns. Once it is done run php artisan migrate command that will create the table with mentioned columns.


Written by
I am a skilled full-stack developer with extensive experience in creating and deploying large and small-scale applications. My expertise spans front-end and back-end technologies, along with database management and server-side programming.

Share on:

Related Posts

Tags: Laravel,