2
votes

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?

System details

  • Operating System OSX
  • PHP Version 7.2
  • Laravel Version 5.5
  • Laravel-Datatables Version 8.0

Update #1

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.

2

2 Answers

3
votes

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.

1
votes

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'
}