O'Reilly logo
  • GIL EG thinks this is interesting:

$book = new Book(); $book->title = "The Alchemist"; // other attributes... $book->save(); // after save() call, an id is created. $category = new Category(); $category->name = "Adventure"; $category->save(); // and now... $category->books->attach($book->id);

From

Cover of Learning Laravel's Eloquent

Note

attach() for ManyToMany related models to be added to the pivot table