3分鐘短文:看傻眼侯勉!Laravel給數(shù)據(jù)庫造假竟成工具鏈

引言

上一章我們了解了使用laravel遷移功能創(chuàng)建數(shù)據(jù)庫表鹦筹,把DBA的工作挪到開發(fā)端,這樣把崗位都省出來了址貌。 但是只有光禿禿的數(shù)據(jù)庫表盛龄,有個(gè)殼子沒有數(shù)據(jù)確實(shí)沒啥用。本文為大家講講laravel怎么給數(shù)據(jù)庫造假數(shù)據(jù),堂而皇之地假裝生產(chǎn)余舶。

img

本文使用的構(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;
?
factory->define(Contact::class, function (Faker\Generatorfaker) {
return [
'name' => faker->name, 'email' =>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)作者

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末质帅,一起剝皮案震驚了整個(gè)濱河市适揉,隨后出現(xiàn)的幾起案子留攒,更是在濱河造成了極大的恐慌,老刑警劉巖嫉嘀,帶你破解...
    沈念sama閱讀 218,858評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件炼邀,死亡現(xiàn)場離奇詭異,居然都是意外死亡吃沪,警方通過查閱死者的電腦和手機(jī)汤善,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來票彪,“玉大人,你說我怎么就攤上這事不狮〗抵” “怎么了?”我有些...
    開封第一講書人閱讀 165,282評論 0 356
  • 文/不壞的土叔 我叫張陵摇零,是天一觀的道長推掸。 經(jīng)常有香客問我,道長驻仅,這世上最難降的妖魔是什么谅畅? 我笑而不...
    開封第一講書人閱讀 58,842評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮噪服,結(jié)果婚禮上毡泻,老公的妹妹穿的比我還像新娘。我一直安慰自己粘优,他們只是感情好仇味,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,857評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著雹顺,像睡著了一般丹墨。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上嬉愧,一...
    開封第一講書人閱讀 51,679評論 1 305
  • 那天贩挣,我揣著相機(jī)與錄音,去河邊找鬼没酣。 笑死王财,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的四康。 我是一名探鬼主播搪搏,決...
    沈念sama閱讀 40,406評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼闪金!你這毒婦竟也來了疯溺?” 一聲冷哼從身側(cè)響起论颅,我...
    開封第一講書人閱讀 39,311評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎囱嫩,沒想到半個(gè)月后恃疯,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,767評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡墨闲,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年今妄,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片鸳碧。...
    茶點(diǎn)故事閱讀 40,090評論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡盾鳞,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出瞻离,到底是詐尸還是另有隱情腾仅,我是刑警寧澤,帶...
    沈念sama閱讀 35,785評論 5 346
  • 正文 年R本政府宣布套利,位于F島的核電站推励,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏肉迫。R本人自食惡果不足惜验辞,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,420評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望喊衫。 院中可真熱鬧跌造,春花似錦、人聲如沸格侯。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽联四。三九已至撑碴,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間朝墩,已是汗流浹背醉拓。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評論 1 271
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留收苏,地道東北人亿卤。 一個(gè)月前我還...
    沈念sama閱讀 48,298評論 3 372
  • 正文 我出身青樓,卻偏偏與公主長得像鹿霸,于是被迫代替她去往敵國和親排吴。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,033評論 2 355