【发布时间】:2018-06-03 21:28:38
【问题描述】:
在我的 laravel 应用程序中,我将一个变量 $data 传递给一个视图,稍后我将包含在另一个视图中。所以在我的控制器方法中,我有:
public function random($id){
$data = DB::table('reports')->where('id',$id);
return view('partials.data', compact('data'));
}
在partials.data我有:
{!! Form::open(['url'=>'reports/data',$id]) !!}
<table class="table table-responsive table-condensed table-bordered tab-content">
<thead>
<tr>
<th>Month</th>
<th>Value</th>
</tr>
</thead>
<tbody>
@foreach($data as $dat)
<tr>{{$dat->month}}</tr>
<tr>{{$dat->value}}</tr>
@endforeach
</tbody>
</table>
{!! Form::close() !!}
在主视图中我有这个功能:
function kpi_values(d) {
// `d` is the original data object for the row
kpi = d.id;
return '@include("reports.data", array("id" => "kpi"))';
}
触发原因:
$('#monthly_table tbody').on('click', 'td.details-controls', 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 {
row.child(kpi_values(row.data())).show();
tr.addClass('shown');
}
});
当我运行它时,我收到以下错误:
ErrorException in 3534c4c98c65c2d5267bf7c54a960d41 line 13:
Undefined variable: data
我已经在局部视图中传递了变量数据,但是,它似乎在主视图中需要它。
有什么方法可以在不将变量传递给主视图的情况下做到这一点?我不想混用,因为局部视图控制器方法需要一个参数,而主视图中没有参数。
【问题讨论】: