Laravel: HasDatatable Trait


Just a simple setup on datatable – to have a trait that can be use for other models.

Assumed that you already install Yajra Datatable, we going to setup the API end point, simplify the setup of datatable.

Here the trait:

You can use the protected $datatable property to select which fields to display in your datatable.

The usage:

The API Endpoint:

Then you are done!

You may overwrite the datatable scope in your model if you need other implementation for the scope.

This is just one neat simple setup, reusable to your other models.

Hope this can help speed setup your datatable API Endpoint.

Thanks for reading!

p/s: You may see this setup in Cleanique Coders

Leave a Reply

Your email address will not be published. Required fields are marked *

three × 1 =