laravel数据表创建修改

1、创建表users表

     

php artisan make:migration create_users_table

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


2、运行命令后,会在 /database/migrations/ 生成对应的数据库迁移文件,通过修改文件里的 up 方法 和 down 文件,来创建数据表和删除数据表

3、运行应用中所有未执行的迁移

      php artisan migrate

注意

在修改列之前,确保已经将doctrine/dbal依赖添加到composer.json文件,Doctrine DBAL 库用于判断列的当前状态并创建对列进行指定调整所需的SQL语句。

你可以使用下列方法添加doctrine/dbal依赖

composer require doctrine/dbal
或者 在composer.json文件中添加 "doctrine/dbal":"v2.5.12"之后 执行 composer update
"require": {
    "php": ">=5.6.4",
    "laravel/framework": "5.3.*",
    "doctrine/dbal":"v2.5.12"
},


4后期若想修改完善之前的数据表 不能再操作之前的文件,修改或删除字段需要一个新的迁移文件

     新增字段     php artisan make:migration add_要添加的字段名_to_添加字段的表名_table --table=要添加字段的表

     修改原有字段属性  php artisan make:migration change_要修改的字段名_on_要修改字段的表名_table --table=要添加字段的表

     (ps: migrate的文件名称 上面那样写只是为了尽量规范而已重要的是迁移文件里面的内容)

     新增字段:

    

    public function up()
    {
        Schema::table('articles', function (Blueprint $table) {
            $table->string('description')->nullable()->after('title');
        });
    }

    public function down()
    {
        Schema::table('articles', function (Blueprint $table) {
            $table->dropColumn('description');
        });
    }

      修改字段:

     

    public function up()
    {
        Schema::table('articles', function (Blueprint $table) {
            $table->string('description', 200)->change();
        });
    }

    public function down()
    {
        Schema::table('articles', function (Blueprint $table) {
            //
        });
    }
5、运行 php artisan migrate 即可生效

    


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章