0

I get an error "htmlspecialchars () expects parameter 1 to be string, array given". I am passing an array from the controller to the view and trying to collect it in a javascript function, Thank you very much for your help.

Code controller

$etis = EtiquetaPresupuesto::where('presupuestos_id', $id)->get();
$idEtiquetas=[];

foreach ($etis as $eti) {
    array_push($idEtiquetas, $eti->etiquetas_id);
}

return view('presupuestos_contabilidad/info-presupuestos', [
    'etiquetas_id' => $idEtiquetas,
]);

Code javascript in view

function etiquetas_sesion(){
    var array_etiquetas = {{$etiquetas_id}};
}

2 Answers 2

3

You must use json_encode() function to get array or object.

Also you can use @json() blade selector

For example:

function etiquetas_sesion(){
    var array_etiquetas = @json($etiquetas_id);
}
function etiquetas_sesion(){
    var array_etiquetas = {!! json_encode($etiquetas_id) !!}
}
Sign up to request clarification or add additional context in comments.

Comments

1

$etiquetas_id in your code is an array, but you're trying to print it as string via {{ }}, which escapes it through htmlspecialchars().

If you want the array to be preserved in JavaScript (as JavaScript array), use json_encode():

var array_etiquetas = {!! json_encode($etiquetas_id) !!};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.