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.

4
  • 1
    You missed a ] after your variable. ;) Commented Apr 26, 2017 at 10:43
  • Possible duplicate of How to pass variables and data from PHP to JavaScript? Commented Apr 26, 2017 at 10:44
  • That's was a mistake here in the question, Its correct there ! Commented Apr 26, 2017 at 10:49
  • Is the extesion of your view .blade.php? Commented Apr 26, 2017 at 10:52

2 Answers 2

4
data: [{
   name: 'Car',
   y: 56.33,
   sliced: true,
   selected: true,
   url: "{{route('dashboard')}}" // note: surround with double quote
}]
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.