New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

ymo/laratables

A package to easily transform your Eloquent models into JSON to be used by Datatables 1.10+.
62 3
Install
composer require ymo/laratables:dev-master
PHP:>=5.4.0
License:MIT
Last Updated:Apr 13, 2015
Links: GitHub  ·  Packagist
Maintainer: Ymo

Laratables

A Laravel package to convert Eloquent queries into JSON output to work with Datatables.

Installation

Add the following to your composer.json file.

require {
	"ymo/laratables": "~2"
}

Run composer install.

Open app/config/app.php and add:

'Ymo\Laratables\LaratablesServiceProvider',

Also add the alias:

'Laratables' => 'Ymo\Laratables\Facades\LaratablesFacade',

And publish the configuration from the command line:

php artisan vendor:publish

Usage

Use the model function to set the main Eloquent model.

$dt = Laratables::model('NewsItem');

Set any optional relationship you want to use.

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

Set the columns you want to retrieve. Columns of relationship models are written as

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

Wrap some optional HTML around a column. Parameters can be passed by wrapping them in curly braces.

$dt->wrapHtml('title', '<a href="https://github.com/yuri-moens/laratables/blob/HEAD/news/{id}/edit">', '</a>');
$dt->wrapHtml('user.first_name', '<a href="users/{user->id}/edit">', '</a>');

Additional columns can also be added manually.

$dt->addColumn('delete','<a class="btn btn-danger" href="https://github.com/yuri-moens/laratables/blob/HEAD/news/{id}" data-method="delete">Delete</a>');

Get the JSON output.

$response = $dt->make();

Chaining

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

Laratables::model('NewsItem') 
	->with('user', 'newsCategory')
	->columns([ 'id', 'title', 'user.first_name', 'newsCategory.name', 'created_at', 'updated_at' ])
	->wrapHtml('title', '<a href="https://github.com/yuri-moens/laratables/blob/HEAD/news/{id}/edit">', '</a>')
	->wrapHtml('user.first_name', '<a href="users/{user->id}/edit">', '</a>')
	->addRawColumn('delete','<a class="btn btn-danger" href="https://github.com/yuri-moens/laratables/blob/HEAD/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.

Related Packages

startupwrench/laratables

A package to easily transform your Eloquent models into JSON to be used by Datat...

51 1
sofa/hookable

Laravel Eloquent hooks system.

2,049,184 65
volosyuk/simple-eloquent

Adaptation of eloquent for retrieving its attributes

117,926 55
gzero/eloquent-tree

Eloquent Tree is a tree model for Laravel Eloquent ORM.

59,480 131
martinbean/laravel-sluggable-trait

A trait you can apply to Eloquent models to have slugs automatically generated o...

49,126 24