zoukankan      html  css  js  c++  java
  • Laravel Migrate

    artisan命令行创建migrate

    格式:

    1 php artisan make:migration YourFileName

    示例:

    1 php artisan make:migration create_books_table

    我们找到laravel目录下databasemigrations2017_XX_XX_XXXXXX_create_books_table.php

    注意:XX代表时间戳,因时而异

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    <?php
    use IlluminateDatabaseSchemaBlueprint;
    use IlluminateDatabaseMigrationsMigration;
    class CreateBooksTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            //
        }
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            //
        }
    }

    这里的CreateBooksTable类继承了Migration,我们看下Migration类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    namespace IlluminateDatabaseMigrations;
    abstract class Migration
    {
        /**
         * The name of the database connection to use.
         *
         * @var string
         */
        protected $connection;
        /**
         * Get the migration connection name.
         *
         * @return string
         */
        public function getConnection()
        {
            return $this->connection;
        }
    }

    这里我们把up函数改写成

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    public function up()
    {
        Schema::create('chs'function (Blueprint $table) {
            $table->increments('id');           
            $table->string('slug')->unique();//additional
            $table->string('title');//additional
            $table->text('content');//additional
            $table->timestamps();
            $table->timestamp('published_at')->index();//additional
        });
    }

    同时参考以下文档,文档选自http://laravelbook.com/laravel-migrations-managing-databases/

    一切准备就绪,我们开始迁移

    1 php artisan migrate

    如果你迁移后发觉并不是你想要的数据表,可以回滚

    1 php artisan migrate:rollback

    这里要给大家提个醒,down方法一定要写,可能有些人这个方法会空着,觉得只要数据表创建出来了就没事了,但是一旦涉及rollback又没有down方法,你的migrate操作就是不可逆的!  

    就像这样:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    <?php
    use IlluminateDatabaseSchemaBlueprint;
    use IlluminateDatabaseMigrationsMigration;
    class CreateCommentsTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('comments'function (Blueprint $table) {
                $table->increments('id');           
                $table->integer('postid');
                $table->string('title');//additional
                $table->text('content');//additional
                //$table->timestamps();
                $table->timestamp('published_at')->index();//additional
            });
        }
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::drop('comments');
        }
    }

    Laravel使用了门面模式

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    namespace IlluminateSupportFacades;
    /**
     * @see IlluminateDatabaseSchemaBuilder
     */
    class Schema extends Facade
    {
        /**
         * Get a schema builder instance for a connection.
         *
         * @param  string  $name
         * @return IlluminateDatabaseSchemaBuilder
         */
        public static function connection($name)
        {
            return static::$app['db']->connection($name)->getSchemaBuilder();
        }
        /**
         * Get a schema builder instance for the default connection.
         *
         * @return IlluminateDatabaseSchemaBuilder
         */
        protected static function getFacadeAccessor()
        {
            return static::$app['db']->connection()->getSchemaBuilder();
        }
    }
  • 相关阅读:
    末学者笔记--KVM虚拟机管理(2)
    末学者笔记--OpenStack介绍(1)
    末学者笔记--openstack共享组件:rabbitmq(3)
    末学者笔记--KVM虚拟化(1)
    末学者笔记--Jenkins+Git+Gitlab+Ansible实现持续集成自动化部署静态网站
    末学者笔记--Gitlab(二)
    末学者笔记--Git介绍(一)
    末学者笔记--Python模块
    末学者笔记--Python函数三玄
    末学者笔记--Python函数二玄
  • 原文地址:https://www.cnblogs.com/wuyuxin/p/7039669.html
Copyright © 2011-2022 走看看