vendredi 24 août 2018

Model Relation not updated

i created a post from a author, and what im doing is fire a event listener when a author reaches a X number of posts, so what im doing is something like:

$author = Author::where('author_id', Auth::user()->id)->first()
 $post = new Tip();
$post->author_id = $author->id;
...
$post-save();

So now imagina that i had already 5 posts before this previous save, in this case i should have 6;

Bu when i call a model method from author:

dd(sizeof($author->posts));

It gives me 5, and i can imagine that this is because the post was save before the instance created from the $author, but i was expecting that when i called "$author->posts" i was creatin a query and give me the current results.

Can someone tell me whats wrong and if Laravel have a approach for these situations besides of creating one new instance after the save and get the posts length?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire