Reputation: 4129
Can't figure out simple task: join 1 table and add column. There is no useful documentation about service implementation here: 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',
];
}
Above will trigger bizarre error
Requested unknown parameter 'manufacturers.id' for row 0, column 0
Tried many variations like:
return [
'id',
];
Above will trigger Column 'id' in field list is ambiguous
Another one was:
return [
[
'name' => 'id',
'data' => 'id'
]
];
this will result in: strtolower() expects parameter 1 to be string, array given
And so on and so forth. Maybe some one just can give basic join example using Service implementation?
This one seams to be closest to working solution:
public function query()
{
$query = Technika::query()
->join('manufacturers','technika.manufacturer_id','=','manufacturers.id')
->select( $this->getColumns() );
return $this->applyScopes($query);
}
and
protected function getColumns()
{
return [
'technika.id',
'manufacturers.title'
];
}
But I'm getting Requested unknown parameter 'technika.id' for row 0, column 0.
However XHR response seams to be ok, I can see correct ata coming from backend.
Upvotes: 2
Views: 4670
Reputation: 4129
Solved problme by doing this:
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 method is used in query() and in html() and they both expect different type of array format. So easest way is to extract name key ant put it to query() select method.
Upvotes: 3
Reputation: 1128
hope this will works for you if not then let me know
$query = Technika::query()
->select($this->getColumns())
->join('manufacturers','technika.manufacturer_id','=','manufacturers.id')
->get();
return $this->applyScopes($query);
protected function getColumns()
{
return 'manufacturers.id'
}
Upvotes: 1