laravel8数据迁移及填充总填充器调用共厂

迁移表: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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值