Laravel-datatables: Displaying only HTML text code in Laravel 5.4

Created on 16 Feb 2017  路  7Comments  路  Source: yajra/laravel-datatables

Summary of problem or feature request

HTML Code in Datatable in Laravel 5.4, not parsing code in webpage, in Laravel 5.3.* is working ok.

The same behavior on your demo page: Eloquent DataTable with Array Response in Action column.


Environment: Laravel 5.4, Acacha\Adminlte

Code snippet of problem

    public function data()
    {
        DB::statement(DB::raw('set @rownum=0'));
        $result = My_Model::select(array(
            DB::raw('@rownum  := @rownum  + 1 AS rownum'),
            'created_at',
            'name'));

        $datatables = Datatables::eloquent($result)
            ->editColumn('rownum', '<div style="text-align:center;"><a class="btn btn-primary btn-xs">{{ $rownum }}</a></div>')
            ->editColumn('created_at','<div style="text-align:center;">{{ App\Functions::setDateTime($created_at) }}')
            ->editColumn('name', '<div style="text-align:left;"><a class="btn btn-success btn-xs">{{ $name}}</a></div>');

        return $datatables->make(true);
    }

Result displayed in datatable in webpage, just this text, not a button with value 1:

<div style="text-align:center;"><a class="btn btn-primary btn-xs">1</a></div>

System details

  • Operating System: Windows 7 Professional
  • PHP Version: 7.0.10 (Laragon 2.2.2)
  • Laravel Version: 5.4.11
  • Laravel-Datatables Version: 7.1.4 & 7.2.1

Most helpful comment

Solved with solution from: #909

Adding rawColumns:
$datatables = Datatables::eloquent($result)->rawColumns(['rownum','created_at','name']) ...

The same behavior on your demo page: Eloquent DataTable with Array Response in Action column. 馃憤

All 7 comments

Solved with solution from: #909

Adding rawColumns:
$datatables = Datatables::eloquent($result)->rawColumns(['rownum','created_at','name']) ...

The same behavior on your demo page: Eloquent DataTable with Array Response in Action column. 馃憤

I couldn't get ->rawColumns([]) to work though, after trying whole day :(. Had to resolve grudgingly to escapeColumns([]).

System details:

  • Operating System: Windows 10
  • PHP Version: 7.0.10
  • Laravel Version: 5.4.17
  • Laravel-Datatables Version: 7.2.0

Any ideas why this might happen? Thanks.

   What's the solution?

public function datatab(User $user)
{

    $users = $user->all();
    return Datatables::of($users)
        ->editColumn('name', function ($model) {
            return '<a href="'.url('/data/users/' . $model->id . '/edit').'">'.$model->name.'</a>';
        })
        ->make(true);

}

whats the solution for this? rawColumn doesn't work. and escapeColumn basically removes HTML tags and only displays the image address. When I use editColumn

->editColumn('image', function ($product_brand) {
                return '<img src="'.$product_brand->image.'" style="height:30px; width:30px" />';
            })

it just shows the html code in the image column

<
img src="https://randomuser.me/api/portraits/men/1.jpg" style="height:30px; width:30px" /
>

Please let me know the solution where I can display this image.
:(

same here @mailnike
any solution ????

The solution for @bayodesegun is escapeColumns([]). This disables the xss protection.

    $users = $user->all();
    return Datatables::of($users)
        ->editColumn('name', function ($model) {
            return '<a href="'.url('/data/users/' . $model->id . '/edit').'">'.$model->name.'</a>';
        })
        ->escapeColumns([])
        ->make(true);

I think using the HTML from laravel collective will also work.

    $users = $user->all();
    return Datatables::of($users)
        ->editColumn('name', function ($model) {
            return Html::link(route('model.edit', $model), $model->name); 
        })
        ->make(true);

->escapeColumns([]) its working

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ahmadbadpey picture ahmadbadpey  路  3Comments

nasirkhan picture nasirkhan  路  3Comments

sangnguyenplus picture sangnguyenplus  路  3Comments

hohuuhau picture hohuuhau  路  3Comments

kamrava picture kamrava  路  3Comments