2

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.

RomkaLTU
  • 3,683
  • 8
  • 40
  • 63

2 Answers2

3

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.

RomkaLTU
  • 3,683
  • 8
  • 40
  • 63
1

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'
}
Shailendra Gupta
  • 1,054
  • 6
  • 15
  • Hi, thanks but no. You can't do get() because applyScopes expects builder instance not collection. And getColumns I need to return more then one column, it should be an array. – RomkaLTU Sep 22 '18 at 08:15
  • you can saprate it by comma (,) you should not retun array – Shailendra Gupta Sep 22 '18 at 08:40