无法解决简单的任务:联接1个表并添加列。这里没有有关服务实现的有用文档:DataTables as a Service Implementation
public function query()
{
$query = Technika::query()
->join('manufacturers','technika.manufacturer_id','=','manufacturers.id')
->select($this->getColumns());
return $this->applyScopes($query);
}
protected function getColumns()
{
return [
'manufacturers.id',
];
}
以上将触发奇怪的错误
Requested unknown parameter 'manufacturers.id' for row 0, column 0
尝试了许多变体,例如:
return [
'id',
];
以上将触发Column 'id' in field list is ambiguous
另一个是:
return [
[
'name' => 'id',
'data' => 'id'
]
];
这将导致:strtolower() expects parameter 1 to be string, array given
等等,依此类推。也许有人可以使用Service实现来给出基本的加入示例?
这个接缝最接近工作解决方案:
public function query()
{
$query = Technika::query()
->join('manufacturers','technika.manufacturer_id','=','manufacturers.id')
->select( $this->getColumns() );
return $this->applyScopes($query);
}
和
protected function getColumns()
{
return [
'technika.id',
'manufacturers.title'
];
}
但是我得到Requested unknown parameter 'technika.id' for row 0, column 0.
无论XHR响应是否正常,我都能看到来自后端的正确数据。
答案 0 :(得分:1)
希望这对您有用,请告诉我
$query = Technika::query()
->select($this->getColumns())
->join('manufacturers','technika.manufacturer_id','=','manufacturers.id')
->get();
return $this->applyScopes($query);
protected function getColumns()
{
return 'manufacturers.id'
}
答案 1 :(得分:1)
解决了以下问题:
protected function getColumns()
{
return [
[ 'data' => 'id', 'name' => 'technika.id', 'title' => 'ID' ],
[ 'data' => 'title', 'name' => 'manufacturers.title', 'title' => 'Manufacturer' ]
];
}
public function query()
{
$query = Technika::query()
->join('manufacturers','technika.manufacturer_id','=','manufacturers.id')
->select( collect($this->getColumns())->pluck('name')->toArray() );
return $this->applyScopes($query);
}
getColumns方法用于query()和html()中,它们都期望使用不同类型的数组格式。因此,最简单的方法是提取名称键ant并将其放入query()select方法。