laravel的迁移总结

migration命令:
一。创建数据表
    1.执行命令,注意:migration后面跟文件名 --create 选择表为了规范文件名称要以create开头《表名》table结束
       
    
1.php artisan make:migration create_lxy_test_table --create=lxy_test
    2.执行后会在database下的migrations中生成一个文件
 
    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 
 
二.在现有的数据表添加字段以docters表为例子
1.执行命令,注意:migration后面跟文件名 --table 选择表
php artisan make:migration docters --table=docters

2.执行后会在database下的migrations中生成一个文件

 
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');
});
}
}
三.在已有表中进行增加,修改,删除操作
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 migrate                              //执行全部迁移文件,此处执行这条命令
Ps:下面的命令当做了解
执行指定的迁移文件
命令:php artisan migrate --path=[迁移文件路径]
如:php artisan migrate --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