0

In the hope that someone could shed some light on the following:

A user has an account and an account has associated cards with it. I am trying to make a seeder to create 5 users with 1 account each of them and then each account to have 1 card associated with it. I am getting multiple triggers on the nested each functions.

So, instead of getting 5 users, 5 accounts, 5 cards, I am getting 5 users, 5 accounts and 15 cards.

Has anyone got any idea as to why the second level of nested function gets triggered so many times? It's like it's accumulating its creations. If I take the Account::factory outside, then I am getting the expected number of triggers.

I can very well change the code to do eventually what I need, but I need to specifically know what is happening in this case of the nested each functions. I'd like to fully understand something rather than skip it and bypass it by doing it some other way.

Here is the seeder code:

$users = User::factory('user')->count(5)->
    create(['password'=>Hash::make('123456789')])->
    each(function($u){
        echo 'created user with id'.$u->id." email=".$u->email."\n\r";
        Account::factory('account')->
            create(['link_to_user_uid'=>$u->id])->
            each(function($a) {
                echo 'created account with id'.$a->id." name=".$a->name."\n\r";
                // Card::factory('card')->create([
                //     'link_to_account_uid' => $a->id,
                // ]);
            });
    });

Here is an echo debug output (which also matches the records also written on the db):

created user with id=1 email=janie.champlin@example.org
        created account with id=1 name=Issac Weimann
created user with id=2 email=jaqueline.kunze@example.org
        created account with id=1 name=Issac Weimann
        created account with id=2 name=Philip Borer
created user with id=3 email=rgislason@example.net
        created account with id=1 name=Issac Weimann
        created account with id=2 name=Philip Borer
        created account with id=3 name=Alexander McGlynn
created user with id=4 email=kaya.osinski@example.org
        created account with id=1 name=Issac Weimann
        created account with id=2 name=Philip Borer
        created account with id=3 name=Alexander McGlynn
        created account with id=4 name=Kitty Cummings
created user with id=5 email=mpagac@example.com
        created account with id=1 name=Issac Weimann
        created account with id=2 name=Philip Borer
        created account with id=3 name=Alexander McGlynn
        created account with id=4 name=Kitty Cummings
        created account with id=5 name=Mr. Ken Rath

Well i managed to achieve the desired functionality by using the magic functions based on the eloquent relationships. I did it like this:

    User::factory('user')->count(5)->
          has(Account::factory()->count(1)->
               hasCards(3))->create();

But still the question remains to be answered i guess as to why i am getting the above unexpected results when using the each method....

aagian
  • 1
  • 2
  • Well i managed to achieve the desired functionality by using the magic functions based on the eloquent relationships. I did it like this: `User::factory('user')->count(5)-> has(Account::factory()->count(1)->hasCards(3))->create();`. But still the question remains to be answered i guess as to why i am getting the above unexpected results when using the each method.... – aagian Mar 15 '23 at 09:57

0 Answers0