我想从laravel控制器传递变量索引以在javascript区域上查看,以便我可以创建图表
我的控制器
<?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'));
}
}
和我的查看文件
@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
在javascript标签上,我想在javascript数据集数据上显示标题,我想显示数字以制成图表。
[当我尝试得到这个并查看我的源代码时
<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>
我需要帮助来解决此问题
使用收集方法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