我想显示一个饼图,其中包含基于预订状态的预订数量。但问题是我的预订表仅包含预订状态 ID。当我尝试获取数据时,它返回错误,其中“饼图应该有字符串类型的第一列”。预订状态的名称位于另一个不同的表上。我对 Laravel 框架很陌生,所以有人可以给我任何从 2 个不同表获取数据的指南吗?这是我的控制器<?phpnamespace 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>
1 回答
RISEBY
TA贡献1856条经验 获得超5个赞
您需要在查询中进行联接。像这样的东西:
$data = DB::table('booking')
->select([
'booking.base_booking_status_id as status_id',
'base_booking_status.name as status_name',
'booking.id as booking_id',
])
->leftJoin('base_booking_status', 'booking.base_booking_status_id', '=', 'base_booking_status.id')
->groupBy('booking.status')
->get();
我对状态表使用了名称“base_booking_status”。您将需要更新它以匹配选择列以及连接的第一个和第三个参数中另一个表的实际名称。您还需要更新要显示的列的名称,我在此处将其称为“名称”。
- 1 回答
- 0 关注
- 105 浏览
添加回答
举报
0/150
提交
取消