Laravel seeder 仅插入随机数的数据然后失败
Laravel seeder inserts only a random number of data and then fails
我有这个播种机功能。它获取所有插入的用户,然后为每个用户生成一个随机数(从 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 个用户的。
我打印了当前正在处理的配置文件图片的id,显然当配置文件图片的数量为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
我已经查看了所有注册用户。我在我的种子文件中又做了一次。这就是它失败的原因。
// 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)
];
});
如果能够将参数传递给工厂就太好了。这样我就可以为每个用户生成随机数量的个人资料图片。但是在这里,呃。
我有这个播种机功能。它获取所有插入的用户,然后为每个用户生成一个随机数(从 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 个用户的。
我打印了当前正在处理的配置文件图片的id,显然当配置文件图片的数量为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
我已经查看了所有注册用户。我在我的种子文件中又做了一次。这就是它失败的原因。
// 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)
];
});
如果能够将参数传递给工厂就太好了