laravel php artisan 数据库操作

1. 利用php artisan 对mysql基本操作
*
*     (1).使用Artisan命令make:migration来创建一个新的迁移:
*          eg:  php artisan make:migration create_users_table
*
*     (2).运行迁移
*          eg: php artisan migrate
*          强行迁移 : php artisan migrate --force
*
*     (3).回滚迁移
*          想要回滚最新的一次迁移”操作“,可以使用rollback命令,注意这将会回滚最后一批运行的迁移,可能包含多个迁移文件:
*          php artisan migrate:rollback
*          migrate:reset命令将会回滚所有的应用迁移:
*
*     (4).在单个命令中回滚/迁移
*          php artisan migrate:refresh
*
*          //结构数据同时迁移
*          php artisan migrate:refresh --seed
*
*     (5).重命名/删除表
*          重命名:$from  原表名   ||   $to   新表名
*              Schema::rename($from, $to);
*
*          删除表:$table  表名
*              Schema::drop($table);
*              Schema::dropIfExists($table);
*
*     (6).创建列/及其列修改
*          使用Schema门面上的table方法
*          eg:  Schema::table('users', function ($table) {
*                    $table->string('email');                                          //创建列
*                    $table->string('description')->nullable()->after('title');        //指定添加位置
*                    $table->drop.$indexName($filedName);                              //删除索引
*                    $table->foreign('user_id')->references('id')->on('users');        //外键约束
*                    $table->dropForeign('posts_user_id_foreign');                     //删除外键
*
*                    "*** 注意:删除指定的列。请记住任何与该列关联的索引也将被删除。***";
*                    $table->dropColumn('email');                                      //删除列
*                    $table->dropColumn('email', 'avatar', 'location');                //删除多列
*                    $table->string('email', 50)->nullable()->change();                //修改属性
*
*                    "*** 注意:enum类型的列的重命名暂不支持。***";
*                    $table->renameColumn('email', 'emails');                          //重命名列
*                });
*
*
*      (7).清除表时先忽略外键约束
*           DB::statement('SET FOREIGN_KEY_CHECKS=0;');
*           App\User::truncate();
*           DB::statement('SET FOREIGN_KEY_CHECKS=1;');
*
*      (8).删除枢纽表的关联数据使用detach
*           eg: User::find(1)->roles()->detach();
*
*          //添加字段名
*      (9).  php artisan make:migration add_要添加的字段名_to_要添加字段的表名_table --table=要添加字段的表名
*
*          //修改字段名
*      (10). 修改原有字段属性  php artisan make:migration change_要修改的字段名_on_要修改字段的表名_table --table=要添加字段的表名
*参考:https://laravel.com/docs/5.6/migrations     http://laravelacademy.org/post/130.html

猜你喜欢

转载自blog.csdn.net/weixin_40076986/article/details/80266403