【发布时间】:2015-09-28 01:43:29
【问题描述】:
我有一个 Laravel 应用程序,我在其中使用 jQuery DataTables (yajra/datatables laravel plugin) 来显示一些记录。我还使用 3 个子行来为他们显示更详细的信息。在子行中,我显示了一些额外的信息、包含每年内容数据的图表以及过去 6 个月的记录值。问题是我不能像填充第一行那样用数据填充最后 2 个子行,因为我不能将所有数据放入 1 个查询中。
这是提供数据表的控制器方法
public function getRowDetails()
{
return view('reports.creates', compact('data'));
}
public function getRowDetailsData()
{
$kpi = $this->getUserActiveKpi();
$data = DB::table('reports')
->orderBy('month','desc')
->groupBy('kpi_id')
->take(5)
->get();
return Datatables::of($kpi, $data)
->make(true);
}
private function getUserActiveKpi(){
$user = Auth::user();
$kpis = DB::table('kpis')
->where('kpi_status',1)
->where('responsible_user', $user->id);
return $kpis;
}
这是初始化脚本:
$(document).ready(function () {
var table;
table = $('#monthly_table').DataTable({
processing: true,
serverSide: true,
dom: "fr<'clear'>Ttip",
ajax: '{{ url("reports/row-details-data") }}',
tableTools: {.....},
columns: [.....],
order: [[1, 'asc']]
});
});
以下是返回子行数据的函数:
var kpi;
function kpi_info(d) {
// `d` is the original data object for the row
return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">'+
'<tr>' +
'<td>KPI:</td>' +
'<td>' + d.kpi_code + '</td>' +
'</tr>' +
'<tr>' +
'<td>Workload:</td>' +
'<td>' + d.kpi_workload + '</td>' +
'</tr>' +
'<tr>' +
'<td>KPI Description:</td>' +
'<td>'+ d.kpi_description + '</td>' +
'</tr>' +
'</table>'
}
function kpi_values(d) {
// `d` is the original data object for the row
kpi = d.id;
// it returns the id, here I want to return the partial view
// that contains the data using the *kpi* as a parameter
return kpi;
}
function kpi_graph(d) {
kpi = d.id;
// it returns the id, here I want to return the partial view that
// contains the graph using the *kpi* as a parameter
return kpi;
}
这是显示它们的功能:
$('#monthly_table tbody').on('click', 'td.details-control', function () {
var tr = $(this).closest('tr');
var row = table.row(tr);
if (row.child.isShown()) {
// This row is already open - close it
row.child.hide();
tr.removeClass('shown');
}
else {
// Open this row
row.child(kpi_data(row.data())).show();
tr.addClass('shown');
}
});
现在我想要实现的是,当我单击显示子行的链接之一时,将 kpi 参数提供给以下控制器方法
public function data($id){
$data = DB::table('reports')
->where('kpi_id',$id)
->orderBy('month','desc')
->take(5)
->get();
return view('reports.data', compact('data'));
}
如果您还有什么需要了解的,请询问,我们将不胜感激所有帮助
编辑
说I cannot put all the data inside 1 query 我的意思是我必须结合3 个查询来做到这一点。首先是:
DB::table('kpis')
->where('kpi_status',1)
->where('responsible_user', $user->id);
第二个是:
DB::table('reports')
->where('kpi_id',$id)
->orderBy('month','desc')
->take(5)
->get();
第三个是:
DB::table('reports')
->where('reports.is_validated',1)
->where('reports.year',$current_year)
->orderBy('month','asc')
->get();
我想不出一个查询可以一次获取所有这些数据。
注意
还有另一个查询,如上一个带有$prev_year 参数的查询。
编辑 2
Schema::create('kpis', function(Blueprint $table)
{
$table->increments('id');
$table->string('kpi_code');
$table->string('kpi_description');
});
Schema::create('reports', function(Blueprint $table)
{
$table->increments('id');
$table->integer('kpi_id')->unsigned()->nullable();
//fk kpi -> values
$table->float('value');
$table->integer('month',false,false,'2');
$table->integer('year',false,false,'4');
});
Schema::table('reports', function($table){
$table->foreign('kpi_id')->references('id')->on('kpis');
});
【问题讨论】:
-
您能解释一下“我不能将所有数据都放在 1 个查询中”是什么意思吗?并举一些例子。
-
@whoacowboy 编辑了问题
-
数据是否相关?你可以利用 Laravel 的 Eloquent。
-
是的,我正在使用 eloquent
-
当我使用
DB::table(...)时,我通常将其保存用于极其复杂的查询。如果您以正确的方式设置模型,则可以执行类似的操作。$kpis = Kpi::with('reports')->where('kpi_status',1)->where('responsible_user', $user->id)->get();您必须展示您的数据如何关联才能帮助您。
标签: php laravel datatables laravel-5