我有一个Laravel应用程序,在这里我使用jQuery DataTables (yajra/datatables插件)来显示一些记录。我还使用3个子行来为它们显示更详细的信息。在子行中,我显示了一些额外的信息,一个包含内容的年度数据的图表,以及过去6个月的记录值。问题是,我不能像填充第一个数据那样,用最后两个子行填充数据,因为我不能将所有数据都放在一个查询中。
下面是为DataTable提供信息的控制器方法
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');
}
});
现在,我试图实现的是,当我单击其中一个显示childrow的链接时,将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();
我想不出一个查询可以同时获得所有这些数据。
Note
还有另一个查询,如带有$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');
});
发布于 2015-07-10 08:34:23
这段代码需要编辑,但希望您能理解。
我会查看Eloquent: Relationships > Defining Relationships > One To Many以及Datables演示站点页面,EloquentController.php - eloquent.getHasMany.title。
Kpi模型
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Kpi extends Model {
/**
* Get the reports for the kpi.
*/
public function reports()
{
return $this->hasMany('App\Report')
->orderBy('month','desc');
}
/**
* Get the Kpis for a specific user
*/
public function scopeForUser($query, $user_id)
{
$query->where('kpi_status',1)
->where('responsible_user', $user_id);
}
}
报表模型
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Report extends Model {
/**
* A report belongs to a kpi
*
* @return mixed
*/
public function kpi()
{
return $this->belongsTo('App\Kpi');
}
/**
* Get the reports for a specific year
*/
public function scopeForYear($query, $year)
{
$query->where('is_validated',1)
->where('year',$year)
->orderBy('month','asc');
}
}
控制器
public function getRowDetails()
{
return view('reports.creates');
}
public function getRowDetailsData()
{
if (Auth::guest())
{
return ['status'=>'error', 'nessage' => 'Unauthorized user.'];
}
$user_id = Auth::user()->id;
$kpis = Kpi::with(['reports' => function($q){
$q->take(5);
})->forUser($user_id)->get();
return Datatables::of($kpis)
->make(true);
}
->take(5)
可能会将查询限制为每个KPI总共只有5个记录,而不是5个报告。如果是这样的话,请查看这篇文章。
按评论更新
它看起来不像你使用的插件提供的嵌套循环
你也许可以这样做,但我从来没有使用过那个插件,所以我不能给你更多的信息。
columns: [
{data: 'kpi_code', name: 'kpi'},
{data: 'kpi_workload', name: 'workload'},
{data: 'kpi_description', name: 'description'},
{data: 'reports[0].name', name: 'report_1'},
{data: 'reports[1].name', name: 'report_2'},
{data: 'reports[2].name', name: 'report_3'},
{data: 'reports[3].name', name: 'report_4'},
{data: 'reports[4].name', name: 'report_5'}
]
https://stackoverflow.com/questions/31321608
复制