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..?

Answers:

Answer

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.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.