引言
上一章我們了解了使用laravel遷移功能創(chuàng)建數(shù)據(jù)庫表鹦筹,把DBA的工作挪到開發(fā)端,這樣把崗位都省出來了址貌。 但是只有光禿禿的數(shù)據(jù)庫表盛龄,有個(gè)殼子沒有數(shù)據(jù)確實(shí)沒啥用。本文為大家講講laravel怎么給數(shù)據(jù)庫造假數(shù)據(jù),堂而皇之地假裝生產(chǎn)余舶。
本文使用的構(gòu)建方式都是寫臟數(shù)據(jù),亂數(shù)據(jù)锹淌,無實(shí)質(zhì)意義的數(shù)據(jù)匿值,只對應(yīng)用程序和數(shù)據(jù)庫有用,別的嘛用沒有赂摆!
代碼時(shí)間
就好比說種地要有種子挟憔,有了種子就可以長出莊稼和糧食;子子孫孫無窮盡也烟号。 數(shù)據(jù)庫也一樣绊谭,數(shù)據(jù)庫表就好比是地,種地我們得有種子汪拥,才能在命令行這樣使用:
<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>
或者讓數(shù)據(jù)庫一切從頭開始达传,舊的數(shù)據(jù)清空,然后填充:
<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>
這是針對所有的遷移和所有的種子文件而言的迫筑,如單獨(dú)指定種子文件宪赶,可以這樣做:
<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>
上一章我們算是把地給準(zhǔn)備好了,下面就開始準(zhǔn)備一袋種子脯燃,使用命令行腳手架指令:
<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 文件內(nèi)添加如下代碼:
<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>
然后手動(dòng)實(shí)現(xiàn) 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>
看到這兒大家應(yīng)該熟悉了吧辕棚,原生的數(shù)據(jù)庫CRUD操作欲主,直接指定表名,并 insert 插入一條新的數(shù)據(jù)逝嚎。就這么簡單扁瓢,就這么一個(gè)動(dòng)作。 但是懈糯,上面的方式涤妒,只能創(chuàng)建一條數(shù)據(jù),我們要造假赚哗,怎么也得大規(guī)模造假她紫,大規(guī)模的數(shù)據(jù)吧。
模型工廠
說一個(gè)高深玄乎的概念吧屿储,聽上去高大上贿讹,其實(shí)用法超簡單。
模型工廠够掠!
這個(gè)是啥民褂,就是生產(chǎn)模型的工廠。模型好比一個(gè)模具,有了模具赊堪,在工廠里可以加工出來數(shù)以千計(jì)數(shù)以萬計(jì)的產(chǎn)品面殖。 模型工廠就是這么個(gè)用法。
還是使用命令行腳手架創(chuàng)建:
<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 目錄下創(chuàng)建一個(gè)php文件 ContactFactory.php哭廉,我們指定是給 contacts 表 對應(yīng)的 Contact 模型文件生產(chǎn)數(shù)據(jù)的脊僚。
在文件內(nèi)添加如下代碼:
<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>
這個(gè)文件為啥長這樣?為什么這么寫遵绰?類不類辽幌,方法不方法,函數(shù)不函數(shù)的椿访!沒錯(cuò)乌企,就是一個(gè)代碼片段,laravel約定的東西成玫, 你這么寫加酵,在執(zhí)行數(shù)據(jù)填充和測試的時(shí)候,它就起作用梁剔!就這么神奇虽画。
那如何控制填充條數(shù)呢?只要在需要填充數(shù)據(jù)的地方調(diào)用工廠類即可:
<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>
這是一條數(shù)據(jù)荣病,可以指定創(chuàng)建 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>
怎么理解呢码撰,就是我們創(chuàng)建了模型工廠類 ContactFactory,然后使用 factory 快捷函數(shù)進(jìn)行實(shí)例化調(diào)用个盆,并使用工廠類內(nèi)的數(shù)據(jù)脖岛, 對每個(gè)模型發(fā)起N次指定方法的調(diào)用。
OK颊亮,模型工廠有很多奇思妙想的玩法柴梆,對于本文就簡單說到這兒吧。
寫在最后
本文介紹了使用laravel的Seeder進(jìn)行數(shù)據(jù)填充终惑,為了高效填充绍在,我們介紹了工廠模式,這個(gè)高級玩意兒雹有,確實(shí)用法有點(diǎn)偏偿渡,本不在五行八卦之列。希望大家用的開心霸奕。
Happy coding :-)
我是@程序員小助手溜宽,專注編程知識,圈子動(dòng)態(tài)的IT領(lǐng)域原創(chuàng)作者