Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - htmlspecialchars() expects parameter 1 to be string, object given

Tags:

php

laravel

I go this error:

htmlspecialchars() expects parameter 1 to be string, object given 

I'm using in controller:

$data = '{"pr":{"code":"1"},"ac":[[{"icon":"web","action":"link","url":"asd"}]]}' $newData = json_decode($data); 

And i send it to the view as array: 'data' => $newData And when i try to use $data into the view, it give me that error

Tried already to use $data->ac OR $data['ac'] but still the same... Some help, please?

like image 642
Kiddo Avatar asked Apr 04 '17 21:04

Kiddo


1 Answers

When you use a blade echo {{ $data }} it will automatically escape the output. It can only escape strings. In your data $data->ac is an array and $data is an object, neither of which can be echoed as is. You need to be more specific of how the data should be outputted. What exactly that looks like entirely depends on what you're trying to accomplish. For example to display the link you would need to do {{ $data->ac[0][0]['url'] }} (not sure why you have two nested arrays but I'm just following your data structure).

@foreach($data->ac['0'] as $link)     <a href="{{ $link['url'] }}">This is a link</a> @endforeach 
like image 117
jfadich Avatar answered Sep 17 '22 11:09

jfadich