code-8
code-8

Reputation: 58810

How do I basically pass a PHP array into a JS array - Laravel 5?

I have a PHP array dd($iperfProfiles);

array:1 [▼
  0 => "ipv6-udp-upload"
]

Trying to access it in JS

I've tried

var iperfProfiles = "{{ json_encode($iperfProfiles) }}";
console.log(iperfProfiles);
return false;

I get

IPERF:1699 ["ipv6-udp-upload"]

How do I basically turn a PHP array into a JS array ?

Upvotes: 0

Views: 81

Answers (3)

Vivek Choudhary
Vivek Choudhary

Reputation: 684

//in PHP 
$iperfProfiles = json_encode($iperfProfiles);

//and then in JS
var iperfProfiles = JSON.parse({!!$iperfProfiles!!});

Upvotes: 1

Elineo
Elineo

Reputation: 104

try with just removing quotes

var iperfProfiles = {{ json_encode($iperfProfiles) }};
console.log(iperfProfiles);

Upvotes: 1

Mohammad Reza
Mohammad Reza

Reputation: 11

just use @json blade directive and pass raw php data to it without encoding it JSON.

in blade template:

const profiles = "@json($profiles)"

Upvotes: 1

Related Questions