Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincent15000's avatar

Model observer with updated() method

Hello,

In an observer, the updated() method has a unique parameter : the updated model.

public function updated(Book $book): void
{
	dd($book->author); // here author is a relationship

    $book->refresh();

	dd($book->author); // here author is a relationship
}

But if I need to access some relationship, I need to refresh the model.

Before and after the refresh, it's not the same author.

Is it normal or is it a bug ?

Thanks for your answers.

V

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

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

1 like

Please or to participate in this conversation.