Learn laravel from scratch (3) data migration and seed factory mass production test data

Time:2021-1-7

1、 Database migration

1. Enter the following command to create the migration file

php artisan make:migration create_admin_user_table --create=admin_user

In this case, the migration file can be modified according to the PHP code

<?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. Enter the following command to migrate the database

docker exec dracohub_myapp_1 php artisan migrate

View the table structure as shown in the figure. The migration is successful
Learn laravel from scratch (3) data migration and seed factory mass production test data

2、 Batch production test data of seed factory

1. Enter the following command to create 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. Enter the following command to create 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. Execute the following command to insert test data

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

As shown in the figure, the test data is inserted successfully
Learn laravel from scratch (3) data migration and seed factory mass production test data

Recommended Today

Using flutter to achieve a lantern layout example code

Walking lantern is a common effect, this article talks about how to usePageViewstayFlutterTo achieve a lantern, the effect is as follows, the height of the current page is higher than other pages, there is a height change animation when switching pages. To achieve this effect, we mainly usePageView.builderComponents.   development  Create home page First, create […]