Skip to content
This repository has been archived by the owner on Feb 12, 2021. It is now read-only.


Folders and files

Last commit message
Last commit date

Latest commit



4 Commits

Repository files navigation

DataTableJson for Laravel

This package aims to simplify the process of creating JSON data for DataTable components.

Recently I started using a Vue DataTable component and found that I needed a simple way of formatting and passing the data into the view to keep everything tidy.


Install the package via composer

    "require": {
      "laralabs/datatablejson": "~1.0.1"     

Once you have updated composer, add the Service Provider to your providers array within config/app.php

'providers' => [

Now you can publish the config file to config/datatablejson.php

php artisan vendor:publish --tag=config

Edit the published configuration file to suit your application.

It is recommended that you change the namespace from the default 'window'


Add the trait to the top of your Eloquent Model

use DataTableJsonTrait;

Add public $columns = [] to your model, this defines the columns that will be used in the table.

Here is an example of a populated $columns array:

    public $columns = [
            "label" => "ID",
            "field" => "id",
            "searchable" => true,
            "orderable" => true
            "label" => "First Name",
            "field" => "first_name",
            "searchable" => true,
            "orderable" => true
            "label" => "Actions",
            "field" => "actions",
            "searchable" => false,
            "orderable" => false,
            "html" => true,
            "content" => '<a class="btn waves-light waves-effect" href="/admin/users/edit/{id}">
                            <i class="fa fa-pencil"></i>

The Actions column in the example shows how you create columns that can include HTML snippets, pull in data from other fields by using {field_name}

Once the columns have been defined you can then create a collection and apply the conversion function.

$users = Users::all();

This will build up the data and prepend it to the view that you specified in the configuration file.

Got a special case and need a different set of fields? You can pass a $columns array to this function which will override the columns specified in the model.

$columns = [
      "....." => "...."
$users = Users::all();

This package is designed to work well with MicroDroid/vue-materialize-datatable


Thanks to Jeffrey Way over at Laracasts for his awesome Laracasts/PHP-Vars-To-Js-Transformer package which allowed me to include the view binding functionality.


Please raise an issue on Github if there is a problem.


This is open-sourced software licensed under the MIT license.


Easily generate DataTable JSON from an eloquent collection and bind it to the view







No packages published