📜  php artisan migrate create table - PHP (1)

📅  最后修改于: 2023-12-03 15:03:34.452000             🧑  作者: Mango

PHP Artisan Migrate Create Table

If you have ever worked with web application development in PHP, you must have come across Laravel - a popular web application framework. Laravel is known for its elegant syntax and extensive support for database integration. One of the many useful features offered by Laravel is migration. In this article, we will explore how to create a table using Laravel's migration utility.

What is Migration in Laravel?

Migration is the process of altering the database schema of a web application without the need to recreate the database from scratch. Laravel provides a migration utility called 'Artisan' which allows developers to create and manage database tables and their relationships.

How to create a table using Laravel's Migration Utility

To create a table using Laravel's Migration Utility, we must first navigate to the root directory of our Laravel application and open the terminal. Once inside the terminal, we will use the 'php artisan' command followed by the 'make:migration' command to create a new migration file. Here is an example:

php artisan make:migration create_table_name --create=table_name

In the above example, 'create_table_name' is the name of the migration file and 'table_name' is the name of the database table we want to create. Laravel will create a new migration file and store it under the 'database/migrations' directory.

We can then open the newly created migration file and write the required code to define the schema of the database table. Here is an example migration file:

<?php

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

class CreateTableName extends Migration
{
    /**
     * Run the migration.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('table_name', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

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

In the above example, the 'up()' method defines the schema of the 'table_name' database table. The schema consists of six columns - 'id', 'name', 'email', 'email_verified_at', 'password', 'remember_token', and 'timestamps'.

Once we have defined the schema, we can use the 'php artisan migrate' command to run the migration.

php artisan migrate

This command will create the 'table_name' database table in our application's database.

Conclusion

In this article, we explored how to create a table using Laravel's migration utility. We learned that Laravel's migration utility allows developers to create and manage database tables and their relationships without the need to recreate the entire database from scratch. We also saw how to define the schema of a database table and run a migration in Laravel. By using Laravel's migration utility, developers can easily manage their application's database schema and easily roll back changes if needed.