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

$book = Book::where('title', '=', 'The Alchemist')->first(); $rightAuthor = Author::where('first_name', '=', 'Paulo')->where('last_name', '=', 'Coelho')->first(); // done! $rightAuthor->books->associate($book);

From

Cover of Learning Laravel's Eloquent

Note

associate() to add id to the related hasMany model