【发布时间】:2016-03-29 14:02:36
【问题描述】:
我在我的项目 lravel 5.1 中使用 datatable yajra 包,想通过 laravel eloquent 获取数据这是我的建议模型代码。
public function candidate()
{
return $this->belongsTo('App\Candidate', 'suggested_user');
}
这是控制器代码。
public function getBCReport()
{
$candidates = \App\Suggestion::with('candidate')->get();
return Datatables::of($candidates)
->make(true);
}
这是我的视图代码:
div class="panel-body">
<div class="candidatereport">
<div class="table-responsive">
<table class="table display" id="table_id" cellspacing="0" width="100%">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</div>
</section>
<script>
$(function() {
$('#table_id').DataTable({
processing: true,
serverSide: true,
dataType: 'json',
ajax: '{!! route('datatables.candidatereport') !!}',
columns: [
{ data: 'candidate.fname', name: 'fname' },
{ data: 'candidate.lname', name: 'lname' },
]
});
});
</script>
当我使用此代码时在控制器中
$candidates = \App\Suggestion::with('candidate');
根据 datatable yajra 文档 http://datatables.yajrabox.com/eloquent/relationships 当我使用时它不起作用
$candidates = \App\Suggestion::with('candidate')->get();
它的工作原理不是根据数据表 yajra 文档。 任何人都可以说出这背后的原因是什么。谢谢
【问题讨论】:
-
get()会给你一个Collection,这是 Datatables 所需要的。
标签: php ajax datatables laravel-5.1