Passing (laravel) Array in Javascript

Passing (laravel) Array in Javascript

    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); ?>;


    Recent posts