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

webfuelcode's avatar

API call error illegal string offset

Working on API project. What's wrong here.

Error shows: Illegal string offset 'socials' on using foreach

"data":{
"game":{
	"name": "some game",
	"id": "1",
	"description": "some description",
	"websiteUrl": "https://website1",
            "socials": [
                {
                    "name": "r/product",
                    "url": "https://website2/r/product/",
                    "type": "reddit"
                },
                {
                    "name": "item/product",
                    "url": "https://github.com/item/product",
                    "type": "github"
                }
}
}
//controller side
..........................
.......................
return view('page', ['collection' => $collection['data']]);

//blade side
@foreach ($collection['game'] as $item)
                {{$item['socials']['name']}}
            @endforeach
0 likes
1 reply
SilenceBringer's avatar

@webfuelcode socials is an array. but game is not, so, this $collection['game'] returns you an array with specified game details, not array of games

@foreach ($collection['game']['socials'] as $social)
		{{ $social['name'] }}
@endforeach

Please or to participate in this conversation.