1.生成建表的 migrate 文件

php artisan make:migration create_posts_table
运行后在 database/migrations 生成 2022_05_15_063003_create_posts_table.php 编写文件如下:

  1. <?php
  2. use Illuminate\Database\Migrations\Migration;
  3. use Illuminate\Database\Schema\Blueprint;
  4. use Illuminate\Support\Facades\Schema;
  5. class CreatePostsTable extends Migration
  6. {
  7. /**
  8. * Run the migrations.
  9. *
  10. * @return void
  11. */
  12. public function up()
  13. {
  14. Schema::create('posts', function (Blueprint $table) {
  15. $table->id();
  16. $table->string('name');
  17. $table->string('email')->unique();
  18. $table->rememberToken();
  19. $table->timestamp('email_verified_at')->nullable();
  20. $table->string('password');
  21. $table->longText('introduction');
  22. $table->timestamps();
  23. });
  24. }
  25. /**
  26. * Reverse the migrations.
  27. *
  28. * @return void
  29. */
  30. public function down()
  31. {
  32. Schema::dropIfExists('posts');
  33. }
  34. }

2.创建工厂类,的同时创建model文件,在工厂类中填充数据

php artisan make:model Models/Post
php artisan make:factory PostFactory
运行后在database/factories 生成 PostFactory.php 文件 编写如下:

<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */

use App\Models\Post;
use Faker\Generator as Faker;
use Illuminate\Support\Str;

$factory->define(Post::class, function (Faker $faker) {
    $date_time = $faker->date . ' ' . $faker->time;
    return [
        'name' => $faker->name,
        'email' => $faker->unique()->safeEmail,
        'remember_token' => Str::random(10),
        'email_verified_at' => now(),
        'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret
        'introduction' => $faker->sentence(),
        'created_at' => $date_time,
        'updated_at' => $date_time,
    ];
});

3.创建Seeders

php artisan make:seeder PostsTableSeeder
在database/seeds/PostsTableSeeder.php添加如下:

<?php

use Illuminate\Database\Seeder;

class PostsTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        factory(App\Models\Post::class)->times(10)->make()->each(function($post,$index){
            $post->save();
        });
    }
}

3.配置工厂类运行

在database/seeders/DatabaseSeeder.php添加如下:

public function run()
{
  // $this->call(UserSeeder::class);
  $this->call(PostsTableSeeder::class);
}

4.生成表和填充数据

运行创建数据表:php artisan migrate
运行填充数据:php artisan db:seed
在执行迁移命令, 生成数据表时, 带上参数即可完成填充数据: php artisan migrate --seed

5.其他相关命令

回滚到最后一批的迁移,这可能会包含多个迁移文件:php artisan migrate:rollback
回滚你应用程序所有的迁移:php artisan migrate:reset

重新创建整个数据库:php artisan migrate:refresh
重建整个数据库并填充数据:php artisan migrate:refresh --seed

使用 —class 选项来指定一个特定的 seeder 类:php artisan db:seed --class=PostsTableSeeder

6.追加字段、修改字段和回滚

追加需要的包:composer require doctrine/dbal
选用2.0版本,默认是3.3.6 修改composer.json文件后,运行composer update
生成修改表结构的文件:php artisan make:migration add_status_to_posts_table编码如下:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddStatusToPostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('posts', function (Blueprint $table) {
            //
            $table->bigInteger('status')->default(0);// 增加字段信息
            $table->string('name',32)->change();// 修改原有字段属性
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('posts', function (Blueprint $table) {
            //
            $table->dropColumn('status');
        });
    }
}

运行:php artisan migrate
回滚:php artisan migrate:rollback