A tutorial on the use of migrate the basis of Laravel learning

  • 2021-08-10 07:03:23
  • OfStack

Preface

As we all know, the current development and testing are all done by multi-person team cooperation, and everyone has a local environment. In the past, we used to add data manually, such as using sql statement to insert data in the database querier. If the data is small, it's easy, but if the data is too large, it hurts, but this is easy in Laravel, and you can use data migration.

This article introduces in detail the related contents about the use of migrate in Laravel, and shares it for your reference and study. The following words are not much to say, let's take a look at the detailed introduction:

Build migration

Command:

Migration


php artisan make:migration create_users_table

Meaning: To create a migration is to create a table named users.

Then you can find a file similar to 2014_10_12_0000_create_users_table. php in the directory database/migrations.
Just as we used to create Table 1 with the php statement before, we can write the fields and constraints of the table we want to create in the file 2014_10_12_000000_create_users_table. php.

The table and create options can be used to specify the table name and whether the migration will create 1 new data table. These options simply follow the above migration command and specify the table name. If you want to specify the custom output path to generate the migration, then execute the make:migration You can use the path option when using the command, and the path provided should be relative to the application root directory.

Migration structure

One migration class contains two methods, up and down.

up mainly contains the specific content of creating tables.

down is the opposite of the former.

Schema::create Accept two parameters. The first is the table name of the table you want to create; The second is a closure (anonymous function) that gets the Blueprint object used to define the new table.

Migration


<?php
 
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
 
class CreateUsersTable extends Migration
{
 /**
  * Run the migrations.
  *
  * @return void
  */
 public function up()
 {
  Schema::create('users', function (Blueprint $table) {
   $table->increments('id');
   $table->string('name');
   $table->string('email')->unique();
   $table->string('password');
   $table->rememberToken();
   $table->timestamps();
  });
 }
 
 /**
  * Reverse the migrations.
  *
  * @return void
  */
 public function down()
 {
  Schema::dropIfExists('users');
 }
}

Running migration

To run all unperformed migrations in the application, use the migrate method of the Artisan command.

Migration


php artisan migrate

Rollback migration

To roll back the latest migration "operation", you can use the rollback command. Note that this will roll back the last batch of migrations run, which may contain multiple migration files:

Migration


php artisan migrate:rollback

migrate: The reset command rolls back all application migrations:

Migration


php artisan migrate:reset

Roll back/migrate in a single command

migrate:refresh The command rolls back all database migrations, and then runs the migrate command. This command can effectively rebuild the entire database:

Migration


php artisan migrate:refresh
php artisan migrate:refresh --seed

Common migration attributes

$table->increments(‘id'); 数据库主键自增 ID
$table->integer(‘votes'); 等同于数据库中的 INTEGER 类型
$table->float(‘amount'); 等同于数据库中的 FLOAT 类型
$table->char(‘name', 4); 等同于数据库中的 CHAR 类型
$table->dateTime(‘created_at'); 等同于数据库中的 DATETIME 类型
$table->enum(‘choices', [‘foo','bar']); 等同于数据库中的 ENUM 类型
$table->tinyInteger(‘numbers'); 等同于数据库中的 TINYINT 类型
$table->timestamps(); 添加 created_at 和 updated_at 列

1 How to write some column name constraints

Migration


Schema::table('users', function ($table) {
 $table->integer('votes')->unsigned(); // Unsigned type 
});

Common constraints

->first() 将该列置为表中第1个列 (仅适用于 MySQL)
->after(‘column') 将该列置于另1个列之后 (仅适用于 MySQL)
->nullable() 允许该列的值为 NULL
->default($value) 指定列的默认值
->unsigned() 设置 integer 列为 UNSIGNED

Summarize


Related articles: