Laravel-htmlspecialchars()期望参数1为字符串,给定对象 [英] Laravel - htmlspecialchars() expects parameter 1 to be string, object given
问题描述
我遇到此错误:
htmlspecialchars() expects parameter 1 to be string, object given
我在控制器中使用
$data = '{"pr":{"code":"1"},"ac":[[{"icon":"web","action":"link","url":"asd"}]]}'
$newData = json_decode($data);
然后将其作为数组发送到视图:'data'=> $ newData 当我尝试在视图中使用$ 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
已经尝试使用$ data-> ac或$ data ['ac'],但仍然相同... 请帮忙吗?
Tried already to use $data->ac OR $data['ac'] but still the same... Some help, please?
推荐答案
使用刀片回波{{ $data }}
时,它将自动转义输出.它只能转义字符串.在您的数据中,$data->ac
是一个数组,而$data
是一个对象,这两个对象都不能原样回显.您需要更具体地说明如何输出数据.看起来到底是什么完全取决于您要完成的工作.例如,要显示链接,您需要做{{ $data->ac[0][0]['url'] }}
(不确定为什么要有两个嵌套数组,但是我只是遵循您的数据结构).
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
这篇关于Laravel-htmlspecialchars()期望参数1为字符串,给定对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!