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

ncltours's avatar

display data into laravel: Cannot use object of type stdClass as array (View

$response = json_decode($response); is =

array:20 [▼ 0 => {#560 ▼ +"AccommodationName": {#430 ▼ +"0": "Barkston Rooms" } +"ResortName": {#561 ▼ +"0": "London" } +"BasicAdultCost": {#562 ▼ +"0": "84.5" } +"OfficialRating": {#563 ▼ +"0": "2" } +"NormalisedName": {#564 ▼ +"0": "BARKSTON ROOMS" } +"Currency": {#565 ▼ +"0": "GBP" } } 1 => {#567 ▼ +"AccommodationName": {#566 ▼ +"0": "Via Hostel Limehouse" } +"ResortName": {#568 ▼ +"0": "London" } +"BasicAdultCost": {#569 ▼ +"0": "89.0" } +"OfficialRating": {#570 ▼ +"0": "2" } +"NormalisedName": {#571 ▼ +"0": "VIA HOSTEL LIMEHOUSE" } +"Currency": {#572 ▼ +"0": "GBP" } } 2 => {#574 ▶} 3 => {#581 ▶} 4 => {#588 ▶} 5 => {#595 ▶} 6 => {#602 ▶}

]

My View:

@foreach($data as $accomodation)

0 likes
5 replies
cmdobueno's avatar

You are using json_decode without any extra parameters. This returns an object. To fix this do the following

$response = json_decode($response, true);
ncltours's avatar

its giving error: { "message": "htmlspecialchars() expects parameter 1 to be string, array given (View:

my display view is:

@foreach($data as $accomodation)

{{ $accomodation['AccommodationName']}}

@endforeach

ncltours's avatar

I am displaying view on ajax success method using:

return view('hotels.partials.hotels', ['data'=> $data])->render();

I

cmdobueno's avatar

you are returning an entire rendered view through ajax?

ncltours's avatar

Yes entire rendered view through ajax

Please or to participate in this conversation.