i just installed "yajra/laravel-datatables": "^1.0" and followed the installation process, and when i try to use the service implementation it failed and gives me this error :
ErrorException (E_ERROR)
Undefined index: attributes (View: /var/www/html/dalilbook/resources/views/admin/city/index.blade.php)
…/vendor/yajra/laravel-datatables-html/src/Html/Builder.php729 :
private function compileTableHeaders()
{
$th = [];
foreach ($this->collection->toArray() as $row) {
$thAttr = $this->html->attributes(array_merge(
array_only($row, ['class', 'id', 'width', 'style', 'data-class', 'data-hide']),
$row['attributes']
));
$th[] = '<th ' . $thAttr . '>' . $row['title'] . '</th>';
}
return $th;
}
// CityController :
public function index(CityDataTable $dataTable)
{
return $dataTable->render('admin.city.index');
}
// admin/city/index.blade.php
{!! $dataTable->table(['class' => 'city-table table table-bordered']) !!}
// CityDataTable:
<?php
namespace App\DataTables;
use App\City;
use Yajra\DataTables\Services\DataTable;
use Yajra\DataTables\EloquentDataTable;
class CityDataTable extends DataTable
{
/**
* Build DataTable class.
*
* @param mixed $query Results from query() method.
* @return \Yajra\DataTables\DataTableAbstract
*/
public function dataTable($query)
{
$dataTable = new EloquentDataTable($query);
return $dataTable
->addColumn('image', function (City $entity) {
return '<div class="avatar" style="background-image: url('.asset($entity->image()).')"></div>';
})
->addColumn('action', 'admin.city.action')
->rawColumns(['id', 'image', 'action', 'name_fr', 'name_ar'])
->editColumn('id', function (City $entity) {
return '<span class="min-width center id">#'.$entity->id().'</span>';
})
->editColumn('name_fr', function (City $entity) {
return '<span class="min-width center">'.$entity->name_fr().'</span>';
})
->editColumn('name_ar', function (City $entity) {
return '<span class="min-width center">'.$entity->name_ar().'</span>';
});
}
/**
* Get query source of dataTable.
*
* @param \App\City $model
* @return \Illuminate\Database\Eloquent\Builder
*/
public function query(City $model)
{
return $model->newQuery()->select($this->getColumns());
}
/**
* 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([
'dom' => 'Bfrtip',
'order' => [[0, 'desc']],
'buttons' => [
'create',
'export',
'print',
'reset',
'reload',
],
]);
}
/**
* Get columns.
*
* @return array
*/
protected function getColumns()
{
return [
'id',
'name_fr',
'name_ar',
];
}
/**
* Get filename for export.
*
* @return string
*/
protected function filename()
{
return 'citydatatable_' . time();
}
}
Hi @yajra I got same problem and I found that in \Yajra\DataTables\Html\Builder::generateScripts function, column->attributes is being unset.
public function generateScripts()
{
$args = array_merge(
$this->attributes, [
'ajax' => $this->ajax,
'columns' => $this->collection->map(function ($column) {
unset($column->attributes);
return $column;
})->toArray(),
]
);
$parameters = $this->parameterize($args);
return new HtmlString(
sprintf($this->template(), $this->getTableAttributes()['id'], $parameters)
);
}
@as247 good catch. Can you please submit a PR for the patch? Thanks!
Ok, I have created PR
@as247 does the patch you submitted fixes this issue? It seems like it does the same thing?
I just tried from scratch and can't replicate the issue
Anyways, I think this might be on the way you call the scripts? Make sure you are calling ->table() first before generating the scripts ->scripts()?
Ok, was able to replicate the issue. And this is indeed on the usage level issue.
You need to generate the table first before issuing the scripts.
{{$dataTable->scripts()}}
{{$dataTable->table(['class' => 'city-table table table-bordered'])}}
@section('content')
{{$dataTable->table(['class' => 'city-table table table-bordered'])}}
@endsection
@push('scripts')
{{$dataTable->scripts()}}
@endpush
Fixed via https://github.com/yajra/laravel-datatables-html/pull/28. v3.0.3.
Kindly update and confirm.
Thanks @as247 !