mardi 23 février 2016

What is the best way to access controller variables within Javascript?

Controller

In my controller, I have an array store in this variable $month_g

dd($month_g);

I got

array:12 [▼
  0 => array:2 [▼
    "up_bytes" => 277664452
    "down_bytes" => 198868462
  ]
  1 => array:2 [▼
    "up_bytes" => 0
    "down_bytes" => 0
  ]
  2 => array:2 [▶]
  3 => array:2 [▶]
  4 => array:2 [▶]
  5 => array:2 [▶]
  6 => array:2 [▶]
  7 => array:2 [▶]
  8 => array:2 [▶]
  9 => array:2 [▶]
  10 => array:2 [▶]
  11 => array:2 [▶]
]

return view('page.index', get_defined_vars());

I want to be able to access this variable in my JS/View.


View

index.blade.php

I've tried

@extends('layouts.internal.master')
@section('content')

..... // content in HTML

@stop

@section('pagescripts')
    <script>
        console.log('{{$month_g}}');
    </script> 
@stop


Result

I kept getting

htmlentities() expects parameter 1 to be string, array given

But console.log() is capable of printing out an array.


Questions

How do I console.log that out ? What did I miss here ?

What is the best way to access controller variables within Javascript ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire