Laravelの移行の概要

移行コマンド:
1。データテーブルを作成する
    1.コマンドを実行します。注:移行の後にファイル名が続きます。ファイル名を標準化するためにテーブルを作成して選択し、「テーブル名」の作成で開始します。テーブルが終了します。
       
    
1.php artisan make:migration create_lxy_test_table --create=lxy_test
    2.実行後、データベースの下の移行でファイルが生成されます
 
    3.ファイルを編集します。次の注記を参照して、記号を変更できます。
        
public function up()
{
    Schema::create('lyh_test', function (Blueprint $table) {
        $table->increments('id');
        // 字符串类型的255位的title字段,注释为‘测试标题’
        $table->string('title',255)->comment('测试标题');
        // text类型的,content字段,注释为‘内容’ 可以为空
        $table->text('content')->comment('内容')->nullable();;
        // time类型的,create_time字段,注释为‘创建时间’
        $table->time('create_time')->comment('创建时间')->nullable();;
        // int类型的,state,注释为'状态1=启用,2=禁用' 默认为0
        $table->integer('state')->default(0)->comment('状态1=启用,2=禁用');
        $table->date('dates')->comment('时间');
        $table->timestamps();
    });
}

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

 

 
4.移行コマンドを実行します
 
php artisan migrate 

 

 
既存のデータテーブルにフィールドを追加します。2.例としてdoctersテーブルを取ります。
1.コマンドを実行します。注:移行の後にファイル名が続きます-テーブル選択テーブル
php artisan make:migration docters --table=docters

2.実行後、データベースの下の移行でファイルが生成されます

 
3.ファイルを開きます
 
  class Docters extends Migration
 {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('docters', function (Blueprint $table) {
 // 需要添加的字段
$table->string('test',100)->comment('需要添加的字段');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('docters', function (Blueprint $table) {
//需要删除的字段
$table->dropColumn('test');
});
}
}
3.既存のテーブルの操作を追加、変更、および削除します
1.ファイルを作成します
php artisan make:migration alter_lxy_test_table

2.操作の追加、変更、削除を行います(注:down()の操作は、ロールバックに使用されるup()の操作と反対にする必要があります)

<?php

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

class AlterLxyTestTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('yuntest', function (Blueprint $table) {
            $table->string('yunn')->nullable()->after('name');//添加
            $table->string('nickname',255)->change();//修改
            $table->dropColumn('created_at');//删除
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('yuntest', function (Blueprint $table) {
            $table->dropColumn('yunn');//删除  与up()中操作相反
            $table->string('nickname',45)->change();//修改
            $table->timestamp('created_at');//添加
        });
    }
}

3.移行コマンドを実行します

php artisan migrate

 

 
4.移行コマンドを実行します
 
コマンド:php artisan merge //すべての移行ファイルを実行し、ここでこのコマンドを実行します
追伸:次のコマンドを理解する必要があります
指定された移行ファイルを実行します
コマンド:php artisan merge --path = [移行ファイルのパス]
如:php職人移行--path = database / migrations / 2020_10_13_153807_docters.php
 
より具体的なコマンドについては、公式ドキュメントを参照して くださいhttps //learnku.com/docs/laravel/6.x/migrations/5173

おすすめ

転載: blog.csdn.net/xiaoyun888_/article/details/109049123