0

I've this tables in my Laravel app: Users Books Categories book_category Authors author_book


im seeding my table wth this code:

factory(App\User::class, 50)->create()->each(function ($user) {
    $user->books()
        ->create(factory(App\Book::class)->make()->toArray())
        ->category()
        ->attach([1,2]);
 });

How can I add author too this code for seed? Is there another way?

Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95
Amin Sh
  • 3
  • 1
  • Not really clear what you're trying to accomplish. You create a book for every user and attach the same two categories for every book. Is there a reason you can't run the attach the same way for the authors? – Devon Bessemer Oct 27 '18 at 12:49
  • thanks Devon, but in the following this code how i attach author? ->author()->attach([1,2]) – Amin Sh Oct 27 '18 at 13:12

1 Answers1

0

I'm guessing you want to add a book to every user. Try creating a new factory for books and then attach $user to your $book.

factory(App\User::class, 50)->create()->each(function ($user) {
    $book = factory(App\Book::class)->create();
    $user->books()->attach($book);
});
Kapitan Teemo
  • 2,144
  • 1
  • 11
  • 25