Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Drewlim7's avatar

How to pass entire array from controller to view in js?

I have an array containing object. Then I am using compact() to pass the whole array and load into a view.

In js,

var array = {{$arrayFromController}}; 

Its not working. It outputs htmlspecialchars() expects parameter 1 to be string, array given....

I saw others suggested to use something like this

@foreach($arrayFromController as $array){
{{$array->name}}
}

However, what i wanted to achieve is to pass the whole array into the js's var.

0 likes
13 replies
Sinnbeck's avatar

Try this instead

var array = {{json_encode($arrayFromController)}};

or

var array = {{$arrayFromController->toJson()}}
Drewlim7's avatar

@sinnbeck Second method is not working. The first method is okay but my code is like messed up and have something like (") in the code that cause failed to load in js.

Here is my partial code. Do i have to decode or encode something before i pass to view?

$path_images = array();

if($detail->path_image1 != null && $detail->path_image1 != ""){
                $image = (object) ['size' => Storage::disk('image')->size($detail->path_image1),'url' => $detail->path_image1];
                array_push($path_images,$image);
            }

            return view('content',compact('detail','path_images'));
Sinnbeck's avatar

Any reason you are casting it to an object ? If you just need to pass it to javascript then dont :)

Drewlim7's avatar

@sinnbeck Actually i wanted to do something like key and value thing with multiple values. Then pass it to js to loop it and populate the data and imgUrl for dropzone to display image.

Sinnbeck's avatar

Well when you pass an array to json_encode it converts 0 indexed arrays to javascript arrays and associative arrays to javascript objects

That means your output will be something like

[
 {
    size: 20, //replace with actual value
    url: http://imadasjdk.com/jds.jpg'', //replace with actual value
 },
{
    size: 55, //replace with actual value
    url: http://imadasjdk.com/dsadasdasdsad.jpg'', //replace with actual value
 },
]
Drewlim7's avatar

@sinnbeck So i have to convert my data to associative arrays from controller then json_encode at js ?

This is what i got from js code.

var method = [{"size":41718,"url":"85\/1qc9FO180s6RYjPVmB4REz88ihfVgRtzhbqbYPMf.png"},{"size":5711,"url":"85\/iEms77w50M3rZFfS8Ck5N5p0L9IcuODoA2IvYx3R.png"},{"size":21252,"url":"85\/Z6fTfz3altsXlBeJs66mNhpsVzzh1AgmasNPZKug.png"}];
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Just remembered that there is a json helper. Sorry I had forgotten

var array = @json($arrayFromController)
Drewlim7's avatar

After I added your code to my side, its not working on blade view. Still showing the error like the one I have provided above.

This is my controller,

$path_images = array();
if($detail->path_image1 != null && $detail->path_image1 != ""){
        $image = ['size' => Storage::disk('image')->size($detail->path_image1),'url' => $detail->path_image1];
        array_push($path_images,$image);
}

$jsonData = collect($path_images)->toJson();

return view('content',compact('detail','jsonData'));

I have dd($jsonData); and it is showing the result format as you stated

"[{"size":41718,"url":"85\/1qc9FO180s6RYjPVmB4REz88ihfVgRtzhbqbYPMf.png"}]" 

JS part,

var method = {{json_encode($jsonData)}};
Sinnbeck's avatar

Did you see my last reply?

var method = @json($jsonData)
1 like
Drewlim7's avatar

Sorry, i think i missed out. Your suggested method at blade view

var method = @json($jsonData)

is working. However i need to modify the data from controller by converting them to associative array instead of collect($array)->toJson();

Thank you very much!

Sinnbeck's avatar

You can change it however you want. Just remember how php to json is done (normal array js array and associative array = js object). Happy to help :)

1 like

Please or to participate in this conversation.