3

I have got this error in my Laravel blade template

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

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

Here is the code

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

Where $silderImageDataArray is the variable and value stored here.

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}]
Aftab H.
  • 1,517
  • 4
  • 13
  • 25
raff
  • 423
  • 4
  • 13
  • 28

1 Answers1

5

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

You may convert it to JSON:

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

If it's a Laravel collection or a model:

var value = '{{ $sliderImageData->toJson() }}';
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • First one is correct for me. There is no error now. But I have not get the targeted result – raff Feb 24 '18 at 11:45
  • @raff your question was about the error. If you want to convert the data to some format, you should have described how exactly are you getting the data and what exactly results you'd like to get. – Alexey Mezenin Feb 24 '18 at 11:47
  • 1
    this is the correct answer, however blade engine replaces `{{ $x }}` by `` not ` ` – azjezz Feb 24 '18 at 12:24