Passing (laravel) Array in Javascript

Created At: 2020-11-07 19:59:43 Updated At: 2020-11-07 20:16:58

There are many different ways you can pass an array to javasscript

1. You can use json_encode()

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

or parse the json string using JSON.parse()

var array = JSON.parse('{{ json_encode($yourarray) }}');

2. You can use laravel json directive

var app = @json($yourarray);

It's very convenient

3. Curly braces and exclamation marks

var array =  {!! json_encode($yourarray) !!};

4. Json parse and encode together

var array = JSON.parse('{!! json_encode($yourarray) !!}');

5. With PHP tag

var app = <?php echo json_encode($yourarray); ?>;

Comment

Add Reviews