php – Laravel播种器仅插入随机数量的数据,然后失败

我有这个Seeder功能.它需要所有插入的用户,然后为每个用户生成一个随机数(从0到12)的个人资料图片.

public function run()
{
    foreach (App\User::all() as $user) {

        echo "Generating profile pic for $user->name ($user->id)... ";
        $profile_pics = factory(App\Image::class, rand(0, 12))->create();

        echo count($profile_pics) . " generated. ";
        echo " Saving... ";

        foreach ($profile_pics as $pp) {
            $user->profile_pics()->save(App\Image::find($pp->id));
        }

        echo "Saved\n";

    }
}

当我运行它时,正确插入了一些第一个随机用户的个人资料图片.但它失败后.

Generating profile pic for Amara Abbott I (1)... 5 generated.  Saving... Saved
Generating profile pic for Major Crist (2)... 12 generated.  Saving... Saved
Generating profile pic for Imelda Hettinger IV (3)... 3 generated.  Saving... Saved
Generating profile pic for Tyrique Von (4)... 5 generated.  Saving... Saved
Generating profile pic for Keshawn Mills IV (5)... 1 generated.  Saving...

  [ErrorException]
  Trying to get property of non-object

在我的数据库中看到,最后插入的个人资料图片是针对第65个用户而不是针对第5个用户.

《php – Laravel播种器仅插入随机数量的数据,然后失败》

我打印了当前处理配置文件pic的id,显然当配置文件pic的数量为1时,它就会失败.

Generating profile pic for Gerald Kohler (10)... 3 generated.  Saving... 56 57 58 Saved
Generating profile pic for Dr. Reagan Donnelly IV (11)... 10 generated.  Saving... 59 60 61 62 63 64 65 66 67 68 Saved
Generating profile pic for Edwardo Leffler (12)... 2 generated.  Saving... 69 70 Saved
Generating profile pic for Jaylen Harris (13)... 11 generated.  Saving... 71 72 73 74 75 76 77 78 79 80 81 Saved
Generating profile pic for Clark Franecki (14)... 5 generated.  Saving... 82 83 84 85 86 Saved
Generating profile pic for Leatha Schiller I (15)... 6 generated.  Saving... 87 88 89 90 91 92 Saved
Generating profile pic for Natasha Stiedemann (16)... 2 generated.  Saving... 93 94 Saved
Generating profile pic for Royce Kshlerin (17)... 11 generated.  Saving... 95 96 97 98 99 100 101 102 103 104 105 Saved
Generating profile pic for Dedric Lang (18)... 8 generated.  Saving... 106 107 108 109 110 111 112 113 Saved
Generating profile pic for Jayden Haag (19)... 2 generated.  Saving... 114 115 Saved
Generating profile pic for Maeve Haley (20)... 4 generated.  Saving... 116 117 118 119 Saved
Generating profile pic for Ana Zboncak (21)... 12 generated.  Saving... 120 121 122 123 124 125 126 127 128 129 130 131 Saved
Generating profile pic for Winnifred Ruecker (22)... 7 generated.  Saving... 132 133 134 135 136 137 138 Saved
Generating profile pic for Guy Fisher Jr. (23)... 9 generated.  Saving... 139 140 141 142 143 144 145 146 147 Saved
Generating profile pic for Adelle Upton (24)... 9 generated.  Saving... 148 149 150 151 152 153 154 155 156 Saved
Generating profile pic for Armando Schamberger MD (25)... 1 generated.  Saving...

  [ErrorException]
  Trying to get property of non-object

怎么会 ?

当只创建一个元素时,结果是什么?

factory(App\Image::class, rand(0, 12))->create();

最佳答案 啊,我刚刚找到了解决方案..问题出在我的ModelFactory.php中我已经查看了所有注册用户.我在Seed文件中再次这样做了.这就是它失败的原因.

// ModelFactory.php
$factory->define(App\User::class, function (Faker\Generator $faker) {
    return [
        'user_id' => App\User::all()->random()->id,
        'base_url' => $faker->imageUrl(350, 350)
    ];
});

能够将参数传递给工厂but unfortunately we can’t会很棒.这样我就可以为每个用户生成一个随机数量的配置文件pic.但在这里,呃.

点赞