先创建模型层以及迁移的表
php artisan make:model Models/User -m
字段
$table->bigIncrements('id'); // 角色 $table->unsignedInteger('role_id')->default(0)->comment('角色ID'); $table->string('username',50)->comment('账号'); $table->string('truename',50)->default('未知')->comment('真实姓名'); $table->string('password',255)->comment('密码'); // nullable 可以为null $table->string('email',50)->nullable()->comment('邮箱'); $table->string('phone',15)->default('')->comment('手机号码'); $table->enum('sex',['先生','女士'])->default('先生')->comment('性别'); $table->char('last_ip',15)->default('')->comment('登录IP'); $table->timestamps(); // 软删除 生成一字段 deleted_at 字段 $table->softDeletes();
执行迁移
php artisan migrate
创建用户的数据填充文件
php artisan make:seeder UserSeeder
生成一个数据工厂
php artisan make:factory UserFactory -m Models/User
auth
//模型层 use Illuminate\Foundation\Auth\User as AuthUser; //database factories use Faker\Generator as Faker; 'username' => $faker->userName, 'truename' => $faker->name(), 'password' => bcrypt('admin888'), 'email' => $faker->email, 'sex' => ['先生','女士'][rand(0,1)]
数据填充修改
User::truncate(); //添加模拟数据 100用户 factory(User::class,200)->create(); User::where('id',1)->update(['username'=>'admin']);
修改时调用
$this->call(User::class);