Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 548
Martney Acha Avatar asked Dec 11 '25 07:12

Martney Acha


2 Answers

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);
    }
like image 195
Govind Samrow Avatar answered Dec 13 '25 19:12

Govind Samrow


I found this issue on github, try adding rawColumns

 Datatables::eloquent($Product)
 ->addColumn(..)
 ->rawColumns(['add_price']);
like image 26
meda Avatar answered Dec 13 '25 21:12

meda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!