htmlspecialchars()期望参数1为字符串,在Laravel中给出数组 [英] htmlspecialchars() expects parameter 1 to be string, array given in Laravel

查看:44
本文介绍了htmlspecialchars()期望参数1为字符串,在Laravel中给出数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Laravel 刀片模板中出现此错误

I have got this error in my Laravel blade template

htmlspecialchars()期望参数1为字符串,给定数组

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

我尝试将数组转换为刀片模板中的字符串.

I have tried to convert the array into string in blade template.

这是代码

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

其中 $ silderImageDataArray 是此处存储的变量和值.

Where $silderImageDataArray is the variable and value stored here.

这是 $ silderImageDataArray

Here is the $silderImageDataArray value

[{"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().并且您正在尝试将数组作为字符串回显.

{{}} will be converted to echo() by Blade template engine. And you're trying to echo the array as a string.

您可以将其转换为JSON:

You may convert it to JSON:

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

如果是Laravel集合或模型:

If it's a Laravel collection or a model:

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

这篇关于htmlspecialchars()期望参数1为字符串,在Laravel中给出数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆