0

I have a highcharts pie chart, Where i want put laravel's route() helper function.

I have the following code, Which

data: [{
    name: 'Car',
    y: 56.33,
    sliced: true,
    selected: true,
    url: {{route('dashboard')}}
}]

Which throws SyntaxError: expected property name, got '{'.

What is the best way to pass PHP variable to JS.

2 Answers2

4
data: [{
   name: 'Car',
   y: 56.33,
   sliced: true,
   selected: true,
   url: "{{route('dashboard')}}" // note: surround with double quote
}]
Murtaza Bhurgri
  • 398
  • 2
  • 9
0

You should open script tag (top or end of your Blade template) and pass your data to JS script.

I usually use @stack. Your code should be like this:

master.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
    <title>@yield('title')</title>
</head>
<body>

    Blah Blah Blah
    <!-- Start Content -->
    @yield('content')
    <!-- End Content -->


    <!-- Custom Scripts -->
    @stack('scripts')
</body>
</html>

chart.blade.php

@extends('master')
@section('content')
    Your Page Content
@endsection

@push('scripts')
    <script>
        var pie = {
            data: [{
                name: 'Car',
                y: 56.33,
                sliced: true,
                selected: true,
                url: "{{route('dashboard')}}"
            }]
        };
    </script>
@endpush

By this way, your data will be replaced with Laravel helper functions.

Erfun
  • 1,079
  • 2
  • 11
  • 26