1

I want to pass variable indexes from laravel controller to view on the javascript area so i can create chart

My Controller

<?php
namespace App\Http\Controllers;
use App\GenderSeries;
use Illuminate\Http\Request;

class EventDashboardController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $titles = GenderSeries::select('titles','numbers')->get();
        return view('events.dashboard',compact('titles'));
    }
}

and my view file

@extends('layouts.templates.event')
@section('title','Store Dashboard')
@section('content')

    <div class="row">
        <div class="col-md-4">
            <div class="card">
                <div class="card-body">
                    <canvas id="genderCount" width="100%" height="80"></canvas>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card">
                <div class="card-body">
                    <canvas id="genderTitle" width="100%" height="80"></canvas>
                </div>
            </div>
        </div>
    </div>
@endsection

@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 !!},
                datasets: [{
                    label: '# Gender Series',
                    data:{!! $titles !!},
                }]
            },
            options: {
                scales: {
                    yAxes: [{
                        ticks: {
                            beginAtZero: true,
                        }
                    }]
                },
            }
        });
    </script>
@endsection

On the javascript labels, I want to display titles on the javascript dataset data I want to display the numbers to make a chart.

when I tried that I got this and view my source code

<script>
        //Show graph of individuals count by gender
        var ctx = document.getElementById('genderCount');
        var myChart = new Chart(ctx, {
            type: 'doughnut',
            data: {
                labels: [{"title":"Feminism","numbers":"50"}],
                datasets: [{
                    label: '# Individual By Gender',
                    data:[{"title":"Feminism","numbers":"50"}],
                }]
            },
            options: {
                scales: {
                    yAxes: [{
                        ticks: {
                            beginAtZero: true,
                        }
                    }]
                },

            }
        });
</script>

I need help to fix this

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

2 Comments

@AntwenySawe dont forget to set it as the answer to your question (and upvote if it's perfect for you)
I already did but on up vote I can't because I still have low reputation for my vot be accepted

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.