以数组格式记录数据到数据库工作者列。然后我会以适当的 JSON 格式返回此数据。我的问题只是从 workers 列的嵌套数组中获取spname值并将其显示在数据表字段视图中。我的代码如下。
接口控制器
public function apiZimmet()
{
/*
test... ok it works
$wdata=Zimmet::first();
$workers = json_decode($wdata->workers , true);
foreach($workers as $key => $value) {
echo $value['spname']."<br>";
}
*/
$data = Zimmet::with('proje:id,name');
return Datatables::eloquent($data)
->editColumn('workers', function ($data) {
$workers = json_decode($data->workers , true);
return $workers;
})
->addColumn('action', function ($data) {
return '<ul class="header-dropdown m-r--5">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
<i class="material-icons">more_vert</i>
</a>
<ul class="dropdown-menu pull-right">
<li>
<a href="/system/stok-personeli/'.$data->id.'/edit" data-toggle="tooltip" data-original-title="Edit">Duzenle</a>
</ul>
</li>
</ul>';
})->make(true);
}
返回 API 文件如下
"data":[
{
"id":"12",
"name":"test",
"sicil_no":"1234",
"proje_id":"1",
"visible":0,
"workers":[
{
"spid":"1",
"spsicil":"35678909",
"spname":"ok"
},
{
"spid":"6",
"spsicil":"123456",
"spname":"sdsf sdfsdf"
}
],
"deleted_at":null,
"created_at":"2020-04-02 23:29:23",
"updated_at":"2020-04-02 23:29:23",
"proje":{
"id":"1",
"name":"deneme"
},
"action":"edit"
}
],
当我在这里调用 workers 列时,出现 [object Object],[object Object] 错误。我想要的是在视图文件中显示 workers 中的 spname 值。
30秒到达战场