迁移表:php artisan make:migration create_users_table
php artisan make:migration create_comment_table
编写字段及数据类型:
public function up() { Schema::create('users', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('username',20)->comment('账号'); $table->string('password',100)->comment('密码'); $table->string('truename',10)->comment('真实姓名'); $table->integer('age')->comment('年龄'); $table->enum('sex',['男','女'])->default('女')->comment('性别'); $table->timestamps(); }); }
public function up() { Schema::create('comment', function (Blueprint $table) { $table->bigIncrements('id'); $table->date('time')->comment('评论时间'); $table->string('content',255)->comment('评论内容'); $table->integer('user_id')->comment('用户id'); $table->timestamps(); }); }
执行迁移:php artisan migrate
创建工厂及模型:php artisan make:factory UserFactory -m User
php artisan make:factory IndexrFactory -m Index
模型层声明表:
class User extends Model { use HasFactory; protected $table='users'; }
class Index extends Model { use HasFactory; protected $table='comment'; }
编写填充规则:
public function definition() { return [ 'username'=>$this->faker->userName, 'password'=>bcrypt('admin'), 'truename'=>$this->faker->name, 'age'=>$this->faker->numberBetween(1,99), ]; }
public function definition() { return [ 'time'=>$this->faker->date, 'content'=>$this->faker->company, 'user_id'=>$this->faker->numberBetween(1,5), ]; }
总填充器调用工厂:
public function run() { User::factory(5)->create(); Index::factory(5)->create(); }
执行填充:
php artisan db:seed