[Chartjs]-How to display variables from Laravel controller to view javascript with specific index

1👍

Use the collection method pluck():

@section('scripts')
    <script>
        //Show graph of individuals count by gender
        var ctx = document.getElementById('genderTitle');
        var myChart = new Chart(ctx, {
            type: 'bar',
            data: {
                labels: {!! $titles->pluck('title') !!},
                datasets: [{
                    label: '# Gender Series',
                    data:{!! $titles->pluck('numbers') !!},
                }]
            },
            options: {
                scales: {
                    yAxes: [{
                        ticks: {
                            beginAtZero: true,
                        }
                    }]
                },
            }
        });
    </script>
@endsection

Leave a comment