zoukankan      html  css  js  c++  java
  • laravel中的数据库迁移

    1.创建数据库迁移文件:生成数据库迁移文件,前面跟着时间戳:

    php  artisan  make:migration  create_posts_table
    

      

    创建数据库迁移文件:可以重命名数据表名: --table和--create后面跟的都是表名:

    php artisan make:migration create_users_table  --table=users
    
    php artisan make:migration create_users_table  --create=users

    在make:migration 后面可以跟:--path  跟上创建迁移文件的路径。

    2.在创建的数据库迁移文件中,编写迁移数据:

    <?php
    
    use IlluminateSupportFacadesSchema;
    use IlluminateDatabaseSchemaBlueprint;
    use IlluminateDatabaseMigrationsMigration;
    
    class CreatePostsTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('posts', function (Blueprint $table) {
                $table->increments('id');
                $table->string('title',100)->default('');
                $table->text('content');
                $table->integer('user_id')->default(0);
                $table->timestamps();
            });
        }
    
        /**
         * Reverse the migrations. reverse:背面;想反  
         * @return void
         */
        public function down()
        {
            Schema::dropIfExists('posts');
        }
    }
    

      

    3.在app/providers/AppServiceProvider.php中填写,Schema的string默认长度,不然执行migrate命令,会报错。因为string的默认长度是1071,而数据库的最长长度小于他,所以会报错。

    public function boot()
        {
            //mb4string
            Schema::defaultStringLength(250);
        }
    

    4.执行数据库迁移命令:

    php artisan migrate
    

      

  • 相关阅读:
    PHP中过滤数组中的元素
    cookie中文乱码解决(php/js)
    Ubuntu系统tar克隆
    磁盘IO性能监控(Linux 和 Windows)
    远程桌面由于帐户限制你无法登录问题
    SAP中关于用户IP信息的获取
    选择界面制作按钮
    ALV常用参数详细描述
    销售订单、外向交货单、交货 bapi
    abap 常用表
  • 原文地址:https://www.cnblogs.com/yiweiyihang/p/8605661.html
Copyright © 2011-2022 走看看