Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lifesound's avatar

Laravel datatables yajra lib , add column

I dont know why add column not working ??


                        <table class="table table-bordered responsive nowrap" id="mats-table"
                               style="width: 100%" ; border="none">
                            <thead>
                            <tr>
                                <th>Id</th>
                                <th>Name</th>
                                <th>Type</th>
                                <th>Location</th>
                                <th>SOH</th>
                                <th>Price</th>
                            </tr>
                            </thead>
                        </table>
<script>
        $(document).ready(function () {

            $(function () {
                $('#mats-table').DataTable({
                    processing: true,
                    serverSide: true,
                    bAutoWidth: false,

                    ajax: '{!! route('materials.data') !!}',
                    columns: [
                        {data: 'id'},
                        {data: 'name'},
                        {data: 'type'},
                        {data: 'location'},
                        {data: 'SOH'},
                        {data: 'price'}
                    ],
                });
            });

        });
    </script>

and the controller


    public function index()
    {
        return view('materials.index', compact('materials'));
    }

    public function data()
    {
        $materials = Material::select(['id', 'name', 'type', 'price', 'SOH', 'location']);

        return Datatables::of($materials)
            ->addColumn('action', function ($query) {
                return '<a href="' . route("materials.show", $query->id) .
                '" class="btn btn-xs btn-primary"><i class="glyphicon glyphicon-edit"></i> Edit</a>';
            })
            ->make(true);
    }
0 likes
4 replies
ctroms's avatar
ctroms
Best Answer
Level 15

I don't have a lot of experience with this package but I am somewhat familiar with datatabels. It could be because you to not have a header column defined for your actions column that you are trying to add. So datatables doesn't know where to add your column data.

Try this.

<table class="table table-bordered responsive nowrap" id="mats-table" style="width: 100%" ; border="none">
    <thead>
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Type</th>
            <th>Location</th>
            <th>SOH</th>
            <th>Price</th>
            <th>Action</th>
        </tr>
    </thead>
</table>
2 likes

Please or to participate in this conversation.