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 即可生效

    


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