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

chavsohi's avatar

counting records in collection

consider below line of codes and it's result:

$user=\auth::user();

$user->vocabulary()->save($cloudy);

$user->vocabulary() ->count(); \ result is 41

$user ->vocabulary ->count(); \ result is 40

true result is 41 , why results is different?

0 likes
3 replies
PatrickSJ's avatar

For the first one you are querying the relationship which actually checks the database. For the second one you already loaded data from the DB before adding a new record. Alas, the data is stale and so need to be refreshed.

$user = $user->refresh();

1 like

Please or to participate in this conversation.