写在前面。我们经常做项目的团队合作开发,每个人都在自己本地的数据库中。如果您曾要求您的同事手动向数据库结构中添加字段,数据库迁移可以解决您的问题。不仅如此,在线部署时,也免去了手动导入数据库或手动修改数据结构的麻烦。数据迁移帮助您方便地维护数据结构。数据填充,使得我们在测试时需要大量的假数据,而不是一个一个地创建数据,我们可以轻松地批量填充大量数据。本文基于Laravel5.5,其他版本类似。数据迁移如果我们需要一个学生表,我们不再使用原生的SQl语句来创建该表。创建迁移文件的前提是已经配置好数据库连接信息。phpartisanmake:migrationcreate_students_table这个命令会在database/migrations/目录下生成一个类似2017_10_28_035802_create_students_table.php的文件。我们在里面添加students表的数据结构迁移。**@returnvoid*/publicfunctionup(){//students是表名Schema::create('students',function(Blueprint$table){//存储引擎$table->engine='InnoDB';//id自增$table->increments('id');//学生姓名$table->string('name');//性别$table->string('sex');//邮箱$table->string('email');//最喜欢的颜色$table->string('favorite_color');//电话号码$table->string('phone');//地址$table->string('addr');//自动维护时间戳$table->timestamps();});}/***反转迁移。**@returnvoid*/publicfunctiondown(){Schema::dropIfExists('students');更多用法请参考官方手册运行迁移phpartisanmigrate,会运行database/migrations/目录下的所有迁移文件,并自动创建一个migrations表记录已经运行过的迁移文件以防止重复运行。让我们看看数据库是否自动创建了学生表。如果出现如下错误:[Illuminate\Database\QueryException]SQLSTATE[42000]:Syntaxerrororaccessviolation:1071Specifiedkeywastoolong;最大密钥长度为767字节(SQL:altertableusersadduniqueusers_email_unique(email))[PDOException]SQLSTATE[42000]:语法错误或访问冲突:1071指定的密钥太长;maxkeylengthis767bytes在database/migrations/目录下,会有laravel自带的users和resetpasswords两个迁移文件。会一起跑。这里我们这样解决,在数据库配置文件config/database中修改mysql下的字符集。戳https://segmentfault.com/a/11...数据填充(支持中文)创建student表Eloquent模型在app目录下创建Student.phpcreate();}}要调用Seeders,我们打开database/seeds/DatabaseSeeder.php文件并将其修改为call(StudentsTableSeeder::类);}}创建模型工厂填写phpartisanmake:factoryStudentsFactory-mStudent这个命令会在database/factories/目录下生成一个StudentsFactory.php文件,我们定义要填写的数据格式define(App\Student::class,function(Faker$faker){$sex=rand(1,1000);返回['name'=>$faker->name,'sex'=>$sex%2==0?'male':'female','email'=>$faker->unique()->safeEmail,'favorite_color'=>$faker->safeColorName,'phone'=>$faker->phoneNumber,'addr'=>$faker->address,];});更多配置请参考vendor/fzaninotto/faker/src/Faker/Generator.php文件让faker填写中文。在app/Providers/AppServiceProvider.php的boot()中添加:publicfunctionboot(){//填写中文数据$this->app->singleton(\Faker\Generator::class,function(){return\Faker\工厂你完成了。如果以上操作都没有报错的话,我们再看看我们的数据库表students表中是否有数据?编号|姓名|性别|电邮|最喜欢的颜色|电话|地址|created_at|updated_at---|------|------|------|------|------|------|------|------|------|---10000|说英语|男|cum_et@example.com|白|17642207316|贵阳海灵|2017-10-2805:19:10|2017-10-2805:19:109999|唐淑珍|男|qlaudantium@example.net|黑|18239453935|南宁友好区|2017-10-2805:19:10|2017-10-2805:19:109998|贾春梅|男|ea35@example.com|苏瑟|17103645128|长沙萧山区|2017-10-2805:19:10|2017-10-2805:19:109997|季志明|男|cdeleniti@example.com|灰色|17002359608|天津华西区|2017-10-2805:19:10|2017-10-2805:19:109996|程艳|男|aspernatur.aut@example.com|黄|17181193397|贵阳西山区2017-10-2805:19:|10|2017-10-2805:19:109995|米博|男|reprehenderit_autem@example.com|紫色|17187328893|广州市东丽区|2017-10-2805:19:10|2017-10-2805:19:109994|LanShulan|Female|et_ea@example.com|Green|18592254358|LanzhouEconomicDevelopmentZone|2017-10-2805:19:10|2017-10-2805:19:109993|LeYao|Female|vel.vitae@example.org|NavyBlue|15891490007|LongtanDistrict,HongKong2017-10-2805:19:|10|2017-10-2805:19:109992|YeZhixin|Female|lcumque@example.net|NavyBlue|15564391466|GaomingDistrict,Beijing|2017-10-2805:19:10|2017-10-2805:19:109991|XuYang|Male|voluptatem00@example.com|Yellow|17097722096|ZhengzhouXinchengDistrict|2017-10-2805:19:10|2017-10-2805:19:109990|LingMin|Female|magni22@example.org|FreshGreen|13021578051|FuchengDistrict,Hangzhou|2017-10-2805:19:10|2017-10-2805:19:109989|XiJian|Female|fugiat_accusantium@example.net|Purple|18070573726|NanchangHailingDistrict|2017-10-2805:19:10|2017-10-2805:19:109988|NieXinhua|Female|debitis_sapiente@example.com|ShuiSe|17004061646|ChengduSouthChangDistrict|2017-10-2805:19:10|2017-10-2805:19:10……Originaltexthttps://www.tech1024.cn/origi...
