Martney Acha Martney Acha - 1 year ago 107
PHP Question

Button in Laravel Datatable not rendering

I'm using Laravel 5.4 and Yajra datatable, below is my code working properly but in the 2nd action I've created, The button is not displaying but instead it display the text itself "

<a href="/product/'. $row->id .'/create-price" class="btn btn-primary">Add Price</a>
" What am I missing ?

public function getProductDatatable()
{
$Product = Product::query();
return Datatables::eloquent($Product)
->addColumn('action', function($row) {
return '<a href="/product/'. $row->id .'/edit" class="btn btn-primary">Edit</a>';
})

->addColumn('add_price', function($row) {
return '<a href="/product/'. $row->id .'/create-price" class="btn btn-primary">Add Price</a>';
})

->make(true);
}


Frontend Part

<script type="text/javascript">
$(function() {
$('#product-table').DataTable({
processing: true,
serverSide: true,
ajax: '{{ url('product/get_product_datatable') }}',
columns : [
{data: 'id', name: 'id'},
{data: 'product_code', name: 'product_code'},
{data: 'action', searchable: false, orderable: false},
{data: 'add_price', searchable: false, orderable: false},
{data: 'created_at', name: 'created_at'},
{data: 'updated_at', name: 'updated_at'}
]
});
});
</script>

Answer Source

You need to define rawColumns :

public function getProductDatatable()
    {
        $Product = Product::query();
       return Datatables::eloquent($Product)
        ->addColumn('action', function($row) {
            return '<a href="/product/'. $row->id .'/edit" class="btn btn-primary">Edit</a>';
        })

        ->addColumn('add_price', function($row) {
            return '<a href="/product/'. $row->id .'/create-price" class="btn btn-primary">Add Price</a>';
        })
       ->rawColumns(['add_price', 'action'])
       ->make(true);
    }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download