引言
上一章我们了解了使用laravel迁移功能创建数据库表,把DBA的工作挪到开发端,这样把岗位都省出来了。 但是只有光秃秃的数据库表,有个壳子没有数据确实没啥用。本文为大家讲讲laravel怎么给数据库造假数据,堂而皇之地假装生产。
本文使用的构建方式都是写脏数据,乱数据,无实质意义的数据,只对应用程序和数据库有用,别的嘛用没有!
代码时间
就好比说种地要有种子,有了种子就可以长出庄稼和粮食;子子孙孙无穷尽也。 数据库也一样,数据库表就好比是地,种地我们得有种子,才能在命令行这样使用:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="php" cid="n9" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">php artisan migrate --seed</pre>
或者让数据库一切从头开始,旧的数据清空,然后填充:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="php" cid="n11" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">php artisan migrate:fresh --seed</pre>
这是针对所有的迁移和所有的种子文件而言的,如单独指定种子文件,可以这样做:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="php" cid="n13" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">php artisan db:seed
php artisan db:seed --class=VotesTableSeeder</pre>
上一章我们算是把地给准备好了,下面就开始准备一袋种子,使用命令行脚手架指令:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="php" cid="n15" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">php artisan make:seeder ContactsTableSeeder</pre>
上述指令会生成文件 ** database/seeds/ContactsTableSeeder.php**。
为了把种子文件引入使用,在 database/seeds/DatabaseSeeder.php 文件内添加如下代码:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="php" cid="n18" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public function run()
{
$this->call(ContactsTableSeeder::class);
}</pre>
然后手动实现 ContactsTableSeeder 类的 run() 方法:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="php" cid="n20" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class ContactsTableSeeder extends Seeder
{
public function run()
{
DB::table('contacts')->insert([
'name' => 'Tom Hanks'
'email' => 'tomhanks@gmail.com',
]);
}
}</pre>
看到这儿大家应该熟悉了吧,原生的数据库CRUD操作,直接指定表名,并 insert 插入一条新的数据。就这么简单,就这么一个动作。 但是,上面的方式,只能创建一条数据,我们要造假,怎么也得大规模造假,大规模的数据吧。
模型工厂
说一个高深玄乎的概念吧,听上去高大上,其实用法超简单。
模型工厂!
这个是啥,就是生产模型的工厂。模型好比一个模具,有了模具,在工厂里可以加工出来数以千计数以万计的产品。 模型工厂就是这么个用法。
还是使用命令行脚手架创建:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="php" cid="n27" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">php artisan make:factory ContactFactory --model=Contact</pre>
上述指令会在 database/factories 目录下创建一个php文件 ContactFactory.php,我们指定是给 contacts 表 对应的 Contact 模型文件生产数据的。
在文件内添加如下代码:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="php" cid="n30" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">use APP\Contact;
use Faker\Generator as Faker;
faker) {
return [
'name' => faker->email,
];
});</pre>
这个文件为啥长这样?为什么这么写?类不类,方法不方法,函数不函数的!没错,就是一个代码片段,laravel约定的东西, 你这么写,在执行数据填充和测试的时候,它就起作用!就这么神奇。
那如何控制填充条数呢?只要在需要填充数据的地方调用工厂类即可:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="php" cid="n33" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">$contact = factory(Contact::class)->create();</pre>
这是一条数据,可以指定创建 500 条:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="php" cid="n35" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">factory(Contact::class, 500)->create();</pre>
怎么理解呢,就是我们创建了模型工厂类 ContactFactory,然后使用 factory 快捷函数进行实例化调用,并使用工厂类内的数据, 对每个模型发起N次指定方法的调用。
OK,模型工厂有很多奇思妙想的玩法,对于本文就简单说到这儿吧。
写在最后
本文介绍了使用laravel的Seeder进行数据填充,为了高效填充,我们介绍了工厂模式,这个高级玩意儿,确实用法有点偏,本不在五行八卦之列。希望大家用的开心。
Happy coding :-)
我是@程序员小助手,专注编程知识,圈子动态的IT领域原创作者