我想显示一个饼图,其中包含基于预订状态的预订数量。但问题是我的预订表仅包含预订状态 ID。当我尝试获取数据时,它返回错误,其中“饼图应该有字符串类型的第一列”。预订状态的名称位于另一个不同的表上。我对 Laravel 框架很陌生,所以有人可以给我任何从 2 个不同表获取数据的指南吗?
这是我的控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
class LaravelGoogleGraph extends Controller
{
function index()
{
$data = DB::table('booking')
->select(
DB::raw('base_booking_status_id as status'),
DB::raw('count(*) as booking_id'))
->groupBy('status')
->get();
$array[] = ['Status', 'Total'];
foreach($data as $key => $value)
{
$array[++$key] = [$value->status, $value->booking_id];
}
return view('google_pie_chart')->with('status', json_encode($array));
}
}
?>
这是我的刀片
<!DOCTYPE html>
<html>
<head>
<title>Make Google Pie Chart in Laravel</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<style type="text/css">
.box{
width:800px;
margin:0 auto;
}
</style>
<script type="text/javascript">
var analytics = <?php echo $status; ?>
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart()
{
var data = google.visualization.arrayToDataTable(analytics);
var options = {
title : 'Booking Status'
};
var chart = new google.visualization.PieChart(document.getElementById('pie_chart'));
chart.draw(data, options);
}
</script>
</head>
<body>
<br />
<div class="container">
<h3 align="center">Booking Report</h3><br />
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Booking Status</h3>
</div>
<div class="panel-body" align="center">
<div id="pie_chart" style="width:750px; height:450px;">
</div>
</div>
</div>
</div>
</body>
</html>
RISEBY