我一直在努力工作并且工作得非常好,但现在我需要打电话给#34; hasMany"关系并将其显示在表格中 这是我的控制者:
public function anyData()
{
$posts = Test::with('cola')->select('tests.*');
return \DataTables::eloquent($posts)
->make(true);}
它给我一个这样的数组(testanyData route):
{
"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警告:table 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>
我想要显示&#34; precio&#34;在th标签&#34; Precio&#34;。
答案 0 :(得分:1)
似乎你的可乐关系是hasMany,因为它返回响应中指示的数组。
"cola": [
{
"test_id": "1",
"tipo": "1",
"precio": "1",
"created_at": null,
"updated_at": null
}
]
在这方面,您还应该将数据作为数组访问。
{data: 'cola.0.precio', name: 'cola.precio'},
或者您可以使用dataTables.js库的render api并循环显示可乐的所有可能值并相应地进行渲染。