久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

向 Laravel 工廠模型添加關(guān)系

Adding Relations to Laravel Factory Model(向 Laravel 工廠模型添加關(guān)系)
本文介紹了向 Laravel 工廠模型添加關(guān)系的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在嘗試添加與工廠模型的關(guān)系以進(jìn)行一些數(shù)據(jù)庫播種,如下所示 - 請注意,我正在嘗試向每個(gè)用戶添加 2 個(gè)帖子

I'm trying to add a relation to a factory model to do some database seeding as follows - note I'm trying to add 2 posts to each user

public function run()
{
   factory(AppUser::class, 50)->create()->each(function($u) {
         $u->posts()->save(factory(AppPost::class, 2)->make());
   });
}

但它拋出以下錯(cuò)誤

Argument 1 passed to IlluminateDatabaseEloquentRelationsHasOneOrMany::s  
ave() must be an instance of IlluminateDatabaseEloquentModel, instance 
of IlluminateDatabaseEloquentCollection given

我認(rèn)為這與保存集合有關(guān).如果通過分別調(diào)用帖子的每個(gè)工廠模型來重新編寫代碼,它似乎可以工作.顯然這不是很優(yōu)雅,因?yàn)槿绻蚁氡A?10 個(gè)或發(fā)布給每個(gè)用戶,那么我必須聲明 10 或行,除非我使用某種 for 循環(huán).

I think its something to do with saving a collection. If re-write the code by calling each factory model for the post separately it seems to work. Obviously this isn't very elegant because if I want to persist 10 or post to each user then I'm having to decalare 10 or lines unless I use some kind of for loop.

public function run()
{
   factory(AppUser::class, 50)->create()->each(function($u) {
     $u->posts()->save(factory(AppPost::class)->make());
     $u->posts()->save(factory(AppPost::class)->make());
   });
}

* 更新 *

有沒有辦法把模型工廠嵌套到第三層深?

Is there any way to nest the model factory a 3rd level deep?

public function run()
{
   factory(AppUser::class, 50)
       ->create()
       ->each(function($u) {
           $u->posts()->saveMany(factory(AppPost::class, 2)
                    ->make()
                    ->each(function($p){
                          $p->comments()->save(factory(AppComment::class)->make());
          }));
   });
}

推薦答案

從 Laravel 5.6 開始就有回調(diào)函數(shù) afterCreating &afterMaking 允許您在創(chuàng)建/制作后直接添加關(guān)系:

Since Laravel 5.6 there is a callback functions afterCreating & afterMaking allowing you to add relations directly after creation/make:

$factory->afterCreating(AppUser::class, function ($user, $faker) {
    $user->saveMany(factory(AppPost::class, 10)->make());
});

$factory->afterMaking(AppPost::class, function ($post, $faker) {
    $post->save(factory(AppComment::class)->make());
});

現(xiàn)在

factory(AppUser::class, 50)->create()

會給你 50 個(gè)用戶,每個(gè)用戶有 10 個(gè)帖子,每個(gè)帖子有一個(gè)評論.

will give you 50 users with each having 10 posts and each post has one comment.

這篇關(guān)于向 Laravel 工廠模型添加關(guān)系的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Magento products by categories(按類別劃分的 Magento 產(chǎn)品)
Resource interpreted as image but transferred with MIME type text/html - Magento(資源被解釋為圖像但使用 MIME 類型 text/html 傳輸 - Magento)
Is there an event for customer account registration in Magento?(Magento 中是否有客戶帳戶注冊事件?)
Magento addFieldToFilter: Two fields, match as OR, not AND(Magento addFieldToFilter:兩個(gè)字段,匹配為 OR,而不是 AND)
quot;Error 404 Not Foundquot; in Magento Admin Login Page(“未找到錯(cuò)誤 404在 Magento 管理員登錄頁面)
Get Order Increment Id in Magento(在 Magento 中獲取訂單增量 ID)
主站蜘蛛池模板: 久久草在线视频 | 91社区在线观看播放 | 免费国产一区 | 久久精品—区二区三区 | 夜夜草 | 国产精品一区二区在线播放 | 精品在线一区二区三区 | 亚洲黄色一级毛片 | 一区二区三区高清不卡 | 牛牛热在线视频 | 精品欧美一区二区三区久久久 | 国产性网 | 一级片免费视频 | 亚洲精品日韩一区二区电影 | 国产www.| 永久免费在线观看 | 麻豆久久久久久久久久 | www.亚洲成人网 | 欧美一级大片免费看 | 国产精品无 | 国产日产精品一区二区三区四区 | 农村真人裸体丰满少妇毛片 | 97视频免费 | 日韩国产精品一区二区三区 | 免费在线精品视频 | 黑人巨大精品欧美一区二区免费 | 精品国产欧美一区二区 | 91视频国产一区 | 亚洲网站在线观看 | 无码一区二区三区视频 | 一级毛片在线播放 | 国产精品久久久久一区二区三区 | 六月色婷 | 中文字幕av在线 | 91视频久久 | 欧美456| 在线播放国产一区二区三区 | 伊人狠狠干 | 中文字幕一区二区三区四区五区 | 在线成人免费av | 中文字幕乱码一区二区三区 |