Search code examples
datatableslaravel-5.4adminlte

Output of Laravel datatable


I am using Laravel 5.4 and Datatable 7.x. I am using AdminLTE also. I need a column named 'Status'. I wrote below code in controller.

return Datatables::of($users)
            ->addColumn('status', function(Users $users){
                $status     =   (($users->status == 1)?
                                    '<a href="" class="btn btn-xs btn-warning"><i class="fa fa-eye"></i></a>'
                                    :
                                    '<a href="" class="btn btn-xs btn-warning"><i class="fa fa-eye-slash"></i></a>'
                                );

                $html       =   '<div class="btn-group">
                                    '.$status.'
                                </div>';

                return $html;
            })
            ->setRowId('id')
            ->make(true);

My output is like below

enter image description here

But I need the button. Can anyone help me in this regard ?


Solution

  • If you want to use html or views in the setColumn method then you need to declare the columns in the rawColumns method so that it doesn't escape the HTML.

     return Datatables::of($users)
                ->addColumn('status', function(Users $users){
                    $status     =   (($users->status == 1)?
                                        '<a href="" class="btn btn-xs btn-warning"><i class="fa fa-eye"></i></a>'
                                        :
                                        '<a href="" class="btn btn-xs btn-warning"><i class="fa fa-eye-slash"></i></a>'
                                    );
    
                    $html       =   '<div class="btn-group">
                                        '.$status.'
                                    </div>';
    
                    return $html;
                })
                ->setRowId('id')
                ->rawColumns(['status'])
                ->make(true);
    

    https://yajrabox.com/docs/laravel-datatables/master/raw-columns