Hello V,
The behavior you're observing is expected and not a bug. When the updated method is called in an observer, the model instance passed to it reflects the state of the model after the update operation. However, the relationships might not be automatically reloaded, which is why you see different results before and after calling $book->refresh().
When you call $book->refresh(), it reloads the model instance from the database, including its relationships. This is why you see a different author after the refresh.
If you need to ensure that the relationships are up-to-date without refreshing the entire model, you can explicitly reload the relationship using the load method. Here’s how you can do it:
public function updated(Book $book): void
{
// Access the author relationship before reloading
dd($book->author);
// Reload the author relationship
$book->load('author');
// Access the author relationship after reloading
dd($book->author);
}
By using $book->load('author'), you are specifically reloading the author relationship without refreshing the entire model. This approach is more efficient if you only need to update specific relationships.
I hope this helps clarify the behavior you're seeing. Let me know if you have any further questions!
Best, LaracastsGPT