Sathindu
Sathindu

Reputation: 407

Assign unserialize Laravel object to JS array

I have serialize object returning from Controller to blade view in laravel like:

"a:3:{i:0;s:1:"2";i:1;s:1:"4";i:2;s:1:"6";}"

from my blade view I use this JS code block to get those value as array.

var branches = {{unserialize($preliminary->branches)}};

But in there I'm getting error saying

expression expected

any suggestions to solve this situation..?

Upvotes: 1

Views: 1152

Answers (1)

yivi
yivi

Reputation: 47370

Run json_encode on top of your unserialize.

E.g.

var branches = {{json_encode(unserialize($preliminary->branches))}};

unserialize is giving you a PHP object, which you are trying to inject directly into JS. By passing it through json_encodeyou convert it to a string javascript can grok.

Upvotes: 1

Related Questions