Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

noblemfd's avatar

Uncaught SyntaxError: Unexpected identifier in Laravel Google Charts API

In my Laravel-5.8, I am trying to apply Google chart API to Draw Pie char for male and female employees

Controller:

public function report()
{

    $data = DB::table('hr_employees')
        ->select(
                DB::raw('gender_id as gender'),
                DB::raw('count(*) as number')
                )
        ->groupBy('gender') 
        ->get();   
    $array[] = ['Gender', 'Number'];
    
    foreach($data as $key => $value) 
    {
        $array[++$key] = [$value->gender, (int)$value->number];
    }        
    
    return view('report-default')
            ->with('gender', json_encode($array));                 
   
}

view

            <div id="pie_chart" style="width:750px; height:450px;">

            </div>

<script src="{{ asset('theme/adminlte3/plugins/jquery/jquery.min.js') }}"></script>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>

<script type="text/javascript">
    var analytics = <?php echo $gender; ?>
    google.charts.load('current', {'packages':['corechart']});
    google.charts.setOnLoadCallback(drawChart);
    function drawChart()
    {
     var data = google.visualization.arrayToDataTable(analytics);
     var options = {
      title : 'Percentage of Male and Female Employee',
      sliceVisibilityThreshold: 0.000000001,
      pieHole: 0.4
     };
     var chart = new google.visualization.PieChart(document.getElementById('pie_chart'));
     chart.draw(data, options);
    }
 </script>

When I ran the application, I got this error:

report-default:814 Uncaught SyntaxError: Unexpected identifier

and this line of code is marked red:

google.charts.load('current', {'packages':['corechart']});

  1. How do I resolve it?

  2. When gender_id (gender) is 1 I want it to display Male and 2 for Female.

Thank you

0 likes
1 reply
devilangelo's avatar

my mistake was semicolon after ?>. after adding it it worked

Please or to participate in this conversation.