Closed rudolfdanza closed 6 years ago
Debugging a little it seems to me that it's calling the Laravel QueryBuilder passing the full getColumns result instead of just the values in data
1st call, successful:
2nd call, unsuccessful:
I also tried specifying titles with this structure:
return ['id' => ['title' => 'ID']];
The columns header in the frontend gets the correct title in both ways but passing the whole array to the Querybuilder without stripping it down to just the database columns plain array throws the error
Putting
if (is_array($value)) {
$value = $value['data'];
}
inside Grammar.php before the stripos call just to try things out of course solves. Tomorrow in my spare time I'll try to trace back calls and pass the stripped down array to the Querybuilder and send out a PR
Without messing up with the code this example solves, it could be refactored but I think it should be added to the examples just in case someone faces this issue:
/**
* Get query source of dataTable.
*
* @param \App\Article $model
* @return \Illuminate\Database\Eloquent\Builder
*/
public function query(Article $model)
{
return $model->newQuery()->select($this->getColumns(true));
}
/**
* Optional method if you want to use html builder.
*
* @return \Yajra\DataTables\Html\Builder
*/
public function html()
{
return $this->builder()
->columns($this->getColumns())
->minifiedAjax()
->addAction(['width' => '80px'])
->parameters($this->getBuilderParameters());
}
/**
* Get columns.
*
* @return array
*/
protected function getColumns($plain = false)
{
$columns = [
[
'data' => 'id',
'name' => 'id',
'title' => 'ID',
],
[
'data' => 'sku',
'name' => 'sku',
'title' => 'SKU',
],
[
'data' => 'name',
'name' => 'name',
'title' => 'Nome',
],
[
'data' => 'description',
'name' => 'description',
'title' => 'Descrizione',
],
[
'data' => 'product_id',
'name' => 'product_id',
'title' => 'Prodotto',
],
[
'data' => 'size_id',
'name' => 'size_id',
'title' => 'Taglia',
],
[
'data' => 'option_id',
'name' => 'option_id',
'title' => 'Variante',
],
[
'data' => 'in_stock',
'name' => 'in_stock',
'title' => 'In Magazzino',
],
[
'data' => 'order',
'name' => 'order',
'title' => 'Ordine',
],
];
if ($plain) {
return array_column($columns, 'data');
} else {
return $columns;
}
}
I am not able to specify column title when using Datatables as a Service
this code works
This does not work
It throws this error in an alert: DataTables warning: table id=dataTableBuilder - Exception Message: stripos() expects parameter 1 to be string, array given
Examining the logs this should be the affected file throwing the exception: ErrorException: stripos() expects parameter 1 to be string, array given in /var/www/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/Grammar.php:910
System details