Laravel Yajra DataTables does not render HTML

Viewed 22

I'm using Laravel Datatables, but the table is not rendering all HTML code. Basically, @method("DELETE") fails to render as HTML, and this is the column I created:

addColumn('action', function($farm) {
        return '<a id="updateBtn" href="/admin/farms/'.$farm->id.'/edit"><i class="bi bi-pencil-fill"></i></a>
                    <form  action="/admin/farms/'.$farm->id.'/delete" method="POST">
                        @method("DELETE")                                   
                        <button id="deleteBtn" type="submit"><i class="bi bi-trash-fill"></i></button>
                    </form>';
        })

I appreciate your help !

1 Answers

You are doing wrong, in your return datatables() method after you do the query requesting for the information that will be on the table, you have to mention how many raw columns the table will have:

        return datatables()->of($query)->addColumn('btn', 
        'btns.delete')->rawColumns(['btn'])->toJson();

In ->addColumn('btn', 'btns.delete') the first param is the name that you will put in the JS columns part, and the second one is the directory where raw column HTML code is located. In the example, it is in public/resources/views/btns/delete.blade.php

In that blade file, you can code the HTML as wanted: delete.blade.php:

<a id="updateBtn" href="/admin/farms/'.$farm->id.'/edit"><i class="bi bi-pencil-fill"></i></a>
                <form  action="/admin/farms/'.$farm->id.'/delete" method="POST">
                    @method("DELETE")                                   
                    <button id="deleteBtn" type="submit"><i class="bi bi-trash-fill"></i></button>
                </form>

Finally, the table will render that column with your HTML code. Hope this help you!

Related