1

一.数据库迁移migration

1.输入以下命令,建立migration文件

php artisan make:migration create_admin_user_table --create=admin_user

这时会在migrations下生成带有日期的migration php文件,代码如下,这只是例子,可以根据具体情况修改

<?php
use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;
class CreateAdminUserTable extends Migration
{
 /**
 * Run the migrations. * * @return void
 */ public function up()
 { Schema::create('admin_user', function (Blueprint $table) {
 $table->id();
 $table->string('username');
 $table->string('password');
 $table->string('email');
 $table->timestamps();
 });
 }
 /**
 * Reverse the migrations. * * @return void
 */ public function down()
 { Schema::dropIfExists('admin_user');
 }
}

2.输入以下命令,进行数据库迁移

docker exec dracohub_myapp_1 php artisan migrate

更新:这里迁移有可能发生错误,运行以下命令大概率可以解决:

composer dump-autoload

查看表结构如图,迁移成功
image

二.seed factory 工厂批量生产测试数据、

1.输入以下命令,创建seeder

php artisan make:seeder AdminUserTableSeeder

AdminUserTableSeeder.php

<?php
use AppModelsadminUser;
use IlluminateDatabaseSeeder;
class AdminUserTableSeeder extends Seeder
{
 /**
 * Run the database seeds. * * @return void
 */ public function run()
 { factory(adminUser::class,50)->create();
 }
}

2.输入以下命令,创建factory

php artisan make:factory adminUserFactory --model=adminUser

adminUserFactory.php

<?php
/** @var IlluminateDatabaseEloquentFactory $factory */
use FakerGenerator as Faker;
$factory->define(AppModelsadminUser::class, function (Faker $faker) {
 return [
 'username' => $faker->userName,
 'password' => $faker->password,
 'email' => $faker->email,
 'created_at' => $faker->dateTime('-1 years'),
 'updated_at' => $faker->dateTimeThisMonth,
 ];
});

3.执行以下命令,插入测试数据

docker exec dracohub_myapp_1 php artisan db:seed --class=AdminUserTableSe
eder

如图,插入测试数据成功
image.png


akazwz
100 声望4 粉丝

Hello World!