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

untymage's avatar

map->only() : Call to a member function only() on array

results: [
    {
    popularity: 32.008,
    vote_count: 0,
    video: false,
    id: 531033,
    adult: false,
    original_language: "en",
    original_title: "I Am Woman",
    genre_ids: [
    18,
    10402
    ],
    title: "I Am Woman",
    vote_average: 0,
    release_date: "2020-09-11"
    },
    {
        popularity: 22.947,
    vote_count: 6,
    video: false,
    id: 492414,
    adult: false,
    original_language: "en",
    original_title: "Alive",
    genre_ids: [
        27,
        53
    ],
    title: "Alive",
    vote_average: 5,
    release_date: "2020-09-18"
    },
]

I want to get only title and release_date key with Collection but it not working:

collect(Http::get($url)->json())->collapse()->take(10)->map->only(['title', 'release_date']);

But:

Error : Call to a member function only() on array

0 likes
5 replies
bobbybouwmann's avatar

Ditch the map call here, it's not doing anything in your case.

2 likes
untymage's avatar

It returns empty collection if i remove the map

untymage's avatar

Anyone else ? isonly() only works on one dimensional array ?

1 like
MichalOravec's avatar

@untymage Try it like this

collect(Http::get($url)->json())->collapse()->take(10)->map(function ($item) {
    return collect($item)->only(['title', 'release_date'])->all();
});
1 like

Please or to participate in this conversation.