3

i want to get sum men learning and women learning from learnings table and display to chart with group by province name.

Error:

 Array to string conversion (View: C:\xampp\htdocs\project\resources\views\chart\index.blade.php)

my controller

 $learnings =DB::table('provinces')
            ->join('learning_province','provinces.id','learning_province.province_id')
            ->join('learnings','learnings.id','learning_province.learning_id')
            ->select('title',\DB::raw('sum(men_learned + women_learned) as sum'))
            ->groupby('title')->whereYear('provinces.created_at', $year)->get();

        $title = [];
        $learning= [];
        foreach ($learnings as $key => $value) {
            $title[$key]=$value->title;
            $learning[$key]=$value->sum;
        }
        return view('home.home', compact('learning', 'title'));

how to display sum men and women with group by province name in this script my script

<script>
        var ctx = document.getElementById('lineChart').getContext('2d');
        var myChart = new Chart(ctx, {
            type: 'bar',
            data: {
                labels:  [{!!$title!!}],
                datasets: [{
                    label: '# ',
                    data: [{!!$learning!!}],
                    backgroundColor: ["#3e95cd"],
                    borderWidth: 1
                }]
            },
            options: {
                scales: {
                    yAxes: [{
                        ticks: {
                            beginAtZero: true
                        }
                    }]
                }
            }
        });
    </script>

1 Answer 1

3

However, instead of manually calling json_encode, you may use the @json Blade directive. The @json directive accepts the same arguments as PHP's json_encode function.

Please like this json_encode in laravel blade

<script>
        var ctx = document.getElementById('lineChart').getContext('2d');
        var myChart = new Chart(ctx, {
            type: 'bar',
            data: {
                labels: @json($title),
                datasets: [{
                    label: '# ',
                    data: @json($learning),
                    backgroundColor: ["#3e95cd"],
                    borderWidth: 1
                }]
            },
            options: {
                scales: {
                    yAxes: [{
                        ticks: {
                            beginAtZero: true
                        }
                    }]
                }
            }
        });
    </script>
Sign up to request clarification or add additional context in comments.

Comments

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.