【发布时间】:2017-10-27 19:49:32
【问题描述】:
我最近一直在使用它,它运行得很好,但现在我需要调用一个“hasMany”关系并将其显示在表格中
这是我的控制器:
public function anyData()
{
$posts = Test::with('cola')->select('tests.*');
return \DataTables::eloquent($posts)->make(true);
}
它给了我一个这样的数组(testanyData 路由):
{
"draw": 0,
"recordsTotal": 1,
"recordsFiltered": 1,
"data": [
{
"id": "1",
"nombre": "Sigrid Mann",
"descripcion": "lPcA0",
"stock": "49",
"imagen": "s14gN",
"created_at": "2017-10-27 18:35:54",
"updated_at": "2017-10-27 18:35:54",
"cola": [
{
"test_id": "1",
"tipo": "1",
"precio": "1",
"created_at": null,
"updated_at": null
}
]
}
],
"queries": [
{
"query": "select count(*) as aggregate from (select '1' as `row_count` from `tests`) count_row_table",
"bindings": [],
"time": 0.6
},
{
"query": "select `tests`.* from `tests`",
"bindings": [],
"time": 0.46
},
{
"query": "select * from `precios` where `precios`.`test_id` in (?)",
"bindings": [
1
],
"time": 0.52
}
],
"input": []
}
这是我的 js:
$(document).ready(function() {
listar();
});
var listar = function () {
var table = $('#productos').DataTable({
"processing": true,
"serverSide": true,
"ajax": "testanyData",
"columns":[
{data:'id'},
{data:'nombre'},
{data:'stock'},
{data: 'cola.precio'},
{defaultContent: "some buttons"}
],
"language": idioma_esp
});
}
但是当我加载视图时,我会收到一个警报:
DataTables 警告:表 id=productos - 请求的未知参数 'cola.precio' 用于第 0 行第 3 列。有关此的更多信息 错误,请看http://datatables.net/tn/4
这些是我在视图中的表:
<div class="row-fluid margin-body">
<table id="productos" class="table table-hover table-condensed">
<thead>
<tr>
<th>Id</th>
<th>Producto</th>
<th>Stock</th>
<th>Precio</th>
<th></th>
</tr>
</thead>
</table>
</div>
我想在标签“Precio”中显示“precio”。
【问题讨论】:
标签: jquery laravel datatables