Weird collections issue with objects

Posted 1 year ago by bart

Hey everybody,

I'm currently having some trouble with the map() method of collections because it looks like it uses the source data as a reference. I will try to explain it by example:

$links = [
    (object) [
        'title' => 'Home',
        'link' => '/',
    (object) [
        'title' => 'About',
        'link' => '/about',
    (object) [
        'title' => 'Imprint',
        'link' => '/imprint',

$newLinks = collect($links)->map(function($link) {
   $link->link = str_replace('/', '#', $link->link);

   return $link;

dd($newLinks, $links);

I would expect that the link property of $newLinks and $links is different but it isn't. When I use arrays instead of stdClass objects it works. Do you have any idea what causes this issue and how to solve it?

Thanks a lot!

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.