Current Location: Home> Latest Articles> How to Create Database Table Structure in Laravel: A Detailed Tutorial and Example

How to Create Database Table Structure in Laravel: A Detailed Tutorial and Example

gitbox 2025-06-30

Introduction to Laravel

Laravel is an elegant and simple PHP web development framework that uses the MVC architecture. It provides powerful tools and services, making it easy to build web applications efficiently. With its extensive scalability and reliability, Laravel is widely used in PHP web development.

Database Table Structure

Designing an efficient database structure is crucial when developing web applications. In Laravel, you can easily create and manage database tables using the Artisan command-line tool. In this tutorial, we will show you how to create a database table structure in Laravel through a specific example.

Creating Migration

Migration in Laravel is used to create and modify database tables. To create a new Migration file, you can use the following command:

php artisan make:migration create_users_table

After running this command, a new Migration file will be generated in the database/migrations folder with a name like 2021_08_12_000000_create_users_table.php. After that, you can run the php artisan migrate command to apply the Migration and update the database.

Writing the Migration

In this example, we will create a table called users, with the following fields:

  • id: Primary Key
  • name: Username
  • email: Email Address
  • password: Password
  • remember_token: Remember Token
  • created_at: Creation Time
  • updated_at: Update Time

Below is the Migration file code for creating the users table:


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

class CreateUsersTable extends Migration
{
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('users');
    }
}

In this Migration file, the up method is used to create the users table, and the down method is used to drop the table.

Executing the Migration

Once the Migration file is written, you can execute the Migration by running the following command to update the database structure:

php artisan migrate

After the command is successfully executed, the users table will be created.

Conclusion

In Laravel, creating and modifying database tables using Migration is very straightforward. First, you create the Migration file, then define the table structure in the up and down methods, and finally, you run the php artisan migrate command to create or update the database table. Laravel’s Migration system makes it easy to manage your database structure, greatly improving development efficiency.