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

向 Laravel 工廠模型添加關系

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

問題描述

我正在嘗試添加與工廠模型的關系以進行一些數據庫播種,如下所示 - 請注意,我正在嘗試向每個用戶添加 2 個帖子

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());
   });
}

但它拋出以下錯誤

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

我認為這與保存集合有關.如果通過分別調用帖子的每個工廠模型來重新編寫代碼,它似乎可以工作.顯然這不是很優雅,因為如果我想保留 10 個或發布給每個用戶,那么我必須聲明 10 或行,除非我使用某種 for 循環.

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 開始就有回調函數 afterCreating &afterMaking 允許您在創建/制作后直接添加關系:

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());
});

現在

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

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

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

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

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

相關文檔推薦

Magento products by categories(按類別劃分的 Magento 產品)
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:兩個字段,匹配為 OR,而不是 AND)
quot;Error 404 Not Foundquot; in Magento Admin Login Page(“未找到錯誤 404在 Magento 管理員登錄頁面)
Get Order Increment Id in Magento(在 Magento 中獲取訂單增量 ID)
主站蜘蛛池模板: 国产一区在线看 | 伊人久久综合 | 成人不卡在线 | 羞羞的视频在线观看 | 成年人在线观看视频 | 日韩欧美一级精品久久 | 亚洲第一av网站 | 国产日韩久久 | 久久在线 | 91最新视频 | 国产一区二区三区欧美 | 毛色毛片免费看 | 国产一区二区三区四区三区四 | 久久免费国产 | 久久久久久久久久久久久9999 | 中文一区二区视频 | www.97zyz.com| www四虎com| 午夜影院操 | 国产精品一区一区 | 中文字幕av一区 | 欧美淫| 国产一区二区三区精品久久久 | 午夜影院在线观看版 | 日韩精品 电影一区 亚洲 | 欧美黄色一区 | 日日天天 | 毛片黄 | 精品一区二区三区在线播放 | 夜夜爆操 | 91精品国产综合久久久久蜜臀 | 国产在线h | 日韩成人在线播放 | 性一爱一乱一交一视频 | 国产福利视频网站 | 久久精品国产一区二区电影 | 在线免费亚洲视频 | 亚洲精品久久久久avwww潮水 | 91精品国产乱码久久久久久久久 | www.亚洲国产精品 | 精品成人av |