3

I wanted to make a post seeder with users and comments as mentioned in the relationship section in the laravel documentation https://laravel.com/docs/5.5/database-testing

  $users = factory(App\User::class, 3)
     ->create()
     ->each(function ($u) {
          $u->posts()->save(factory(App\Post::class)
            ->create()
            ->each(function($p){
                $p->comments()->save(factory(App\Comment::class,5)
                  ->create()
                  ->each(function($c){
                      $c->user()->save(factory(App\User::class)
                        ->make()
                      );
                  })
                );
              })
            );
        }
    );

Expected output was to have 3 users having posts with 5 comments each.

But error occurred:

In Builder.php line 2459: Call to undefined method Illuminate\Database\Query\Builder::save()

Shehzad
  • 337
  • 4
  • 25
glupeksha
  • 460
  • 1
  • 6
  • 14

3 Answers3

1

try this :

$users = factory(App\User::class, 3)
     ->create()
     ->each(function ($u) {
          $u->posts()->save(factory(App\Post::class)->make())
            ->each(function($p){
                $p->comments()->save(factory(App\Comment::class,5)->make())
                  ->each(function($c){
                      $c->user()->save(factory(App\User::class)
                        ->make()
                      );
                  })
                );
              })
            );
        }
    );

Solution with regular foreach

$users = factory(App\User::class, 3)->create();

foreach($users as $user){
    $post = $user->posts()
                          ->create(factory(App\Post::class)->make()->toArray());
    $post->comments()
                     ->createMany(
                               factory(App\Comment::class, 5)
                      ->make(['user_id' => factory(App\User::class)
                               ->create()->id])->toArray());
}
Mathieu Ferre
  • 4,246
  • 1
  • 14
  • 31
  • Erf, I just edited your code, the problem was that you were trying to use the save() method on the factory created Model. I think it would be less complicated with regular foreach – Mathieu Ferre Dec 12 '17 at 15:31
  • I edit My solution, maybe not working as I can't test it right away, but you can get the general idea :) – Mathieu Ferre Dec 12 '17 at 15:35
  • Your solution didnt work.. But i got what i did wrong.. thaanks.. In your solution we have to use arrays inside create method.. isnt it.. – glupeksha Dec 12 '17 at 15:58
  • haaa yes ! indeed ! was it just a missing ->toArray() ? I edited my answer, but if you have a working solution, you can edit it as well :) – Mathieu Ferre Dec 12 '17 at 16:22
  • It says Integrity constraint violation: 1062 Duplicate entry '1' for key 'PRIMARY' for the above code.. – glupeksha Dec 12 '17 at 17:08
0

There are several problems in your solution for example

$p->comments()->save(factory(App\Comment::class,5)

you are trying to save multiple comments with save() which will result in error you should use saveMany() instead. However the solution to your problem can be this:

$users = factory(App\User::class, 3)->create()
        ->each(function ($user) {
            $user->posts()->saveMany(factory(App\Post::class, 5)->make());
        });
foreach ($users as $user){
  foreach ($user->posts as $post){
    $post->comments()->saveMany(factory(App\Comment::class, 5)->make());
  }
}

this will work as expected.

Shehzad
  • 337
  • 4
  • 25
0

try this method:

        $users = factory(App\User::class, 5)->create();

    foreach ($users as $user) {
        $articles = factory(App\Article::class, 5)->create([
            'user_id' => $user->id
        ]);

        foreach ($articles as $article) {
            factory(App\Comment::class, 5)->create([
                'article_id' => $article['id']
            ]);
        }
    }
VeRJiL
  • 415
  • 4
  • 13