2017 © Pedro Peláez
 

library laratables

A package to easily transform your Eloquent models into JSON to be used by Datatables 1.10+.

image

startupwrench/laratables

A package to easily transform your Eloquent models into JSON to be used by Datatables 1.10+.

  • Thursday, January 5, 2017
  • by startupwrench
  • Repository
  • 1 Watchers
  • 1 Stars
  • 9 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Laratables

A Laravel package to convert Eloquent queries into JSON output to work with Datatables. This is a fork of: https://github.com/yuri-moens/laratables to fix some issues, and errors for a production app, feel free to use it, but no warranties expressed or implied., (*1)

Installation

Add the following to your composer.json file., (*2)

require {
    "startupwrench/laratables": "dev-master"
}

Run composer install., (*3)

Open app/config/app.php and add:, (*4)

Startupwrench\Laratables\LaratablesServiceProvider::class,, (*5)

Also add the alias:, (*6)

'Laratables' => Startupwrench\Laratables\LaratablesFacade::class,, (*7)

And publish the configuration from the command line:, (*8)

php artisan vendor:publish, (*9)

Usage

Use the model function to set the main Eloquent model., (*10)

$dt = Laratables::model('NewsItem');, (*11)

Set any optional relationship you want to use., (*12)

$dt->with('user', 'category');, (*13)

Set the columns you want to retrieve. Columns of relationship models are written as ., (*14)

$dt->columns([ 'id', 'title', 'user.first_name', 'newsCategory.name', 'created_at', 'updated_at' ]);, (*15)

Wrap some optional HTML around a column. Parameters can be passed by wrapping them in curly braces., (*16)

$dt->wrapHtml('title', '<a href="news/{id}/edit">', '</a>');
$dt->wrapHtml('user.first_name', '<a href="users/{user->id}/edit">', '</a>');

Additional columns can also be added manually., (*17)

$dt->addColumn('delete','<a class="btn btn-danger" href="news/{id}" data-method="delete">Delete</a>');, (*18)

Get the JSON output., (*19)

$response = $dt->make();, (*20)

Chaining

The property setters all return the L4EloquentDatatables object so you can easily chain commands. The aforementioned example can be rewritten as following., (*21)

Laratables::model('NewsItem') 
    ->with('user', 'newsCategory')
    ->columns([ 'id', 'title', 'user.first_name', 'newsCategory.name', 'created_at', 'updated_at' ])
    ->wrapHtml('title', '<a href="news/{id}/edit">', '</a>')
    ->wrapHtml('user.first_name', '<a href="users/{user->id}/edit">', '</a>')
    ->addRawColumn('delete','<a class="btn btn-danger" href="news/{id}" data-method="delete">Delete</a>')
    ->make();

Limitations

This is a fairly simple package with very few features. It was made for personal use but I figured other people might have a use for it. The most important limitation in my opinion is that you cannot sort columns of relationship models. If anyone is interested in writing support for this, you are more than welcome to do so., (*22)

The Versions

05/01 2017

dev-master

9999999-dev https://github.com/startupwrench/laratables

A package to easily transform your Eloquent models into JSON to be used by Datatables 1.10+.

  Sources   Download

MIT

The Requires

 

laravel eloquent datatables