Laravel-datatables: Eloquent DataTables casts all attributes to string

Created on 23 May 2018  路  11Comments  路  Source: yajra/laravel-datatables

Eloquent DataTables casts all attributes to string

Eloquent DataTables casts all attributes to string, even int ids, even if explicit eloquent model $casts is set.

Code snippet of problem

System details

  • Linux Mint 18.3
  • PHP 7.0.30-0ubuntu0.16.04.1
  • Laravel Version 5.5.40
  • Laravel-Datatables Version 8.6.0
  • Mysql 5.5.72-ubuntu0.16.04.1
for review help wanted

Most helpful comment

Fixed on v8.13.2, thanks to @ridaamirini!

All 11 comments

I encountered this problem today as well. Wondering why this would be happening?

What I'm getting:

image

What I expect:

image

Found the related issue/pr: https://github.com/yajra/laravel-datatables-buttons/pull/67.

ATM, I think you need to use fractal integration for proper API response but I also agree that we can improve the package not to casts all data to string and let the eloquent casting handle it.

Can't check this further though but will definitely implement this when I got the chance. If any of you can, please do not hesitate to submit a PR. Thanks!

@yajra I added a PR

Fixed on v8.13.2, thanks to @ridaamirini!

What have happened with that? It seems it has been reverted in version 9! DataProcessor.php
It seems this PR from @ridaamirini has been discarded. @yajra ?
```php
protected function escapeColumns(array $output)
{
return array_map(function ($row) {
if ($this->escapeColumns == '*') {
$row = $this->escapeRow($row);
} elseif (is_array($this->escapeColumns)) {
$columns = array_diff($this->escapeColumns, $this->rawColumns);
foreach ($columns as $key) {
Arr::set($row, $key, e(Arr::get($row, $key)));
}
}

        return $row;
    }, $output);
}

`

It was reverted due to a breaking change and still unresolved. https://github.com/yajra/laravel-datatables/issues/1951. Will try to have a look at this again when I got the chance.

Any updates?

It was reverted due to a breaking change and still unresolved. #1951. Will try to have a look at this again when I got the chance.

any updates??

@rilwanmsm @fayaz668 I don't think this will be fixed...

Can't check this further atm. Maybe you guys can provide some tests / feedbacks on https://github.com/yajra/laravel-datatables/issues/1951?

@yajra , @ridaamirini below methods works for me

return \DataTables::eloquent(Users::where('isActive',1)) ->rawColumns(['id']) ->make(true);

Was this page helpful?
0 / 5 - 0 ratings

Related issues

josiahke picture josiahke  路  3Comments

vipin733 picture vipin733  路  3Comments

techguydev picture techguydev  路  3Comments

macnux picture macnux  路  3Comments

ahmadbadpey picture ahmadbadpey  路  3Comments