htmlspecialchars() 期望参数 1 为字符串,Laravel 中给出的数组

htmlspecialchars() expects parameter 1 to be string, array given in Laravel

我的 Laravel blade 模板出现了这个错误

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

我试图在 blade 模板中将数组转换为字符串。

这里是代码

<script>
    var value = {{ $sliderImageDataArray }}.toString()
    window.MedicPressSliderCaptions = value;
</script>

其中$silderImageDataArray是这里存储的变量和值。

这是$silderImageDataArray

[{"title":"First title","text":"<p><span id=\"hs_cos_wrapper_module_1498510869515998\" class=\"hs_cos_wrapper hs_cos_wrapper_widget_container hs_cos_wrapper_type_widget_container\" data-hs-cos-general-type=\"widget_container\" data-hs-cos-type=\"widget_container\">First title<\\/span><\\/p>\n<p><a class=\"btn  btn-secondary\" href=\"http:\\/\\/localhost\\/sencare\\/book-appoinment\\/\" target=\"_self\">  Make An Appointment <\\/a>\u00a0<a class=\"btn  btn-light\" href=\"http:\\/\\/localhost\\/sencare\\/our-doctors\\/\" target=\"_self\"> Our Doctors<\\/a><\\/p>\n","is_video":false},{"title":"Second","text":"<p><span id=\"hs_cos_wrapper_module_1498510869515998\" class=\"hs_cos_wrapper hs_cos_wrapper_widget_container hs_cos_wrapper_type_widget_container\" data-hs-cos-general-type=\"widget_container\" data-hs-cos-type=\"widget_container\">Second<\\/span><\\/p>\n<p><a class=\"btn  btn-secondary\" href=\"http:\\/\\/localhost\\/sencare\\/book-appoinment\\/\" target=\"_self\">  Make An Appointment <\\/a>\u00a0<a class=\"btn  btn-light\" href=\"http:\\/\\/localhost\\/sencare\\/our-doctors\\/\" target=\"_self\"> Our Doctors<\\/a><\\/p>\n","is_video":false}]

{{}} 将被 Blade 模板引擎转换为 echo()。而您正试图将数组作为字符串回显。

您可以将其转换为 JSON:

var value = '{{ json_encode($sliderImageDataArray) }}';

如果是 Laravel collection 或模型:

var value = '{{ $sliderImageData->toJson() }}';