This works, all posts are dumped with the votes property set correctly (if it's null then it's updated to have the value 0):
$posts = Post::all();
foreach($posts as $post) {
$post->priority = $post->priority ?? 0;
}
dd($posts);
But if I do this I get an empty collection back:
$posts = Post::all();
$posts = $posts->map(function ($post) {
$post->priority = $post->priority ?? 0;
});
dd($posts);
From the docs it says that map returns a new collection instance and that if you want to modify the existing collection you should use transform, but that produces the same result.