Laravel seeder inserts only a random number of data and then fails

David picture David · Jan 17, 2016 · Viewed 7.5k times · Source

I have this Seeder function. It takes all inserted users and then, for each of them, generate a random number (from 0 to 12) of profile picture.

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";

    }
}

When I run it, some first random user's profile pic are correctly inserted. But after it fails.

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

See in my database, the last inserted profile picture is for the 65th user and not for the 5th.

enter image description here

I printed the id of the current processing profile pic, and apparently when number of profile pic is 1 then it fails.

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

How come ?

What is the result of this when only one element is created ?

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

Answer

David picture David · Jan 17, 2016

Ah, I just found the solution myself.. The problem was in my ModelFactory.php I already look through all registered users. I did it once again in my Seed file. That is why it failed.

// 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)
    ];
});

It would be great to be able to pass parameters to factories but unfortunately we can't. That way I would be able to generate a random number of profile pic per users. But here, ugh.