Laravel 6.X 数据库迁移 创建表 与 修改表

数据库迁移创建表

本篇文章中使用的是mysql数据库,其他数据库需要修改env文件和app配置,请其他地方搜索一下就会找到。


创建示例

1.创建users表:

命令行键入

  1 php artisan make:migration create_users_table //users复数

  1 php artisan make:migration create_users_table --create=users//--create及对应的表名 users 

执行命令后,会在 /database/migrations/ 文件夹下生成对应的数据库迁移文件,通过修改文件里的up 方法和 down方法;

  • 当我们运行迁移时,up 方法会被调用。
  • 当我们回滚迁移时,down 方法会被调用。
  1   public function up()
  2     {
  3         Schema::create('users', function (Blueprint $table) {
  4             $table->bigIncrements('id');
  5             $table->string('name')->unique();
  6             $table->string('email')->unique();
  7             $table->timestamp('email_verified_at')->nullable();
  8             $table->string('password');
  9             $table->rememberToken();
 10             $table->timestamps();
 11         });
 12     }

2.创建一个数据模型的时候 需要同时创建对应迁移文件可以加上 –m:

  1 php artisan make:model Models/Moment -m 

就在Models文件夹下创建了Moment数据模型 -m在 database/migrations创建了对应的数据库迁移文件。【注:类似的还有-f【工厂类】 –c【控制器】,可以合用-cfm,-cm等】

保存后执行

  1 php artisan migrate


会创建对应up方法中配置的字段的数据表。

【上面up方法中创建一个名为users的数据表,表包含id,name,email,email_verified_at,password,rememberToken[注:记住我选项被选中时生成],created_at,updated_at字段】


数据库迁移创建表


今后若想修改完善之前的已经创建好的数据表 不能再操作之前的数据库迁移文件。

修改 新增或删除字段需要创建一个新的数据库迁移文件:

操作前

请检查你的composer.json文件中已经添加好了doctrine/dbal依赖,Doctrine DBAL库是用来确定数据表中字段当前状态,及用于实现目标调整的SQL查询语句的创建工作的,安装依赖请在控制台中执行:

  1 composer require doctrine/dbal

安装完成后,接下来:

新增字段

在控制台中执行命令:

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

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

为示例向users表中添加一个post_id字段,所以我们的命令如下:

  1 php artisan make:migration add_post_id_to_users_table --table=users

然后会在database/migrations创建了对应的数据库迁移文件***_add_post_id_to_users_table.php:

  1 public function up()
  2     {
  3         Schema::table('users', function (Blueprint $table) {
  4             $table->bigInteger('post_id')->nullable()->after('email');
  5         });
  6     }
  7 
  8     public function down()
  9     {
 10         Schema::table('users', function (Blueprint $table) {
 11             $table->dropColumn('post_id');
 12         });
 13     }

【注意方法里已经不是Schema::create方法的调用,而是Schema::table方法的调用了。】

然后,运行

  1  php artisan migrate
即可生效

修改原有字段属性

如果是修改,就调用change方法即可,比如想增加name 字段的字符串长度:

  1 Schema::table('users', function (Blueprint $table) {
  2     $table->string('name', 50)->change();
  3 });

比如将字段修改为可空:

  1 Schema::table('users', function (Blueprint $table) {
  2     $table->string('name', 50)->nullable()->change();
  3 });
然后,运行
  1  php artisan migrate
即可生效


修改字段名

  1 Schema::table('users', function (Blueprint $table) {
  2     $table->renameColumn('from', 'to');//from字段改名为to字段
  3 });
然后,运行
  1  php artisan migrate
即可生效

删除字段

如果目的是删除之前已经存在的表的某些字段,新建数据库迁移文件后,在该文件的up方法中键入如下代码;

  1 Schema::table('users', function (Blueprint $table) {
  2     $table->dropColumn('votes');//删除users表的votes字段
  3 });
  4 
  5 Schema::table('users', function (Blueprint $table) {
  6     $table->dropColumn(['votes', 'avatar', 'location']);//删除users表里面的votes,avatar,location这三个字段
  7 });
然后,运行
  1  php artisan migrate
即可生效
原文地址:https://www.cnblogs.com/dzkjz/p/12369331.html