PHPackages                             msieprawski/resource-table - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. msieprawski/resource-table

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

msieprawski/resource-table
==========================

Alternative for Laravel DataTable

v0.71(10y ago)615107[1 issues](https://github.com/msieprawski/resource-table/issues)PHPPHP &gt;=5.4.0

Since May 12Pushed 10y ago12 watchersCompare

[ Source](https://github.com/msieprawski/resource-table)[ Packagist](https://packagist.org/packages/msieprawski/resource-table)[ RSS](/packages/msieprawski-resource-table/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (15)Used By (0)

Resource Table
==============

[](#resource-table)

About
-----

[](#about)

This Laravel package has been created as a alternative for DataTable. It doesn't use AJAX or any JavaScript. It's very light and scalable. Use it for generating table with data without paying attention to searching/sorting/paginating results. It'll do it for yourself! I'll do my best to develop it all the time because I'll be using it on my projects.

TO DO
-----

[](#to-do)

- default value for searchable columns
- more searchable columns types *(date, datetime, range)*
- add some tests

Compatibility
-------------

[](#compatibility)

Currently package is compatible with Laravel 5

Feature overview
----------------

[](#feature-overview)

- supporting Eloquent ORM and Fluent Query Builder
- ability to join tables and sort results by joined columns
- searchable columns - select or text fields!
- supporting filter callbacks
- custom pagination layouts *(called presenters in Laravel 5)*
- translations
- more coming...

Installation
------------

[](#installation)

Add the following to your `composer.json` file:

```
"msieprawski/resource-table": "*"
```

Then register this service provider with Laravel:

```
'Msieprawski\ResourceTable\ResourceTableServiceProvider',
```

and add class alias for easy usage

```
'ResourceTable' => 'Msieprawski\ResourceTable\ResourceTable',
```

Don't forget to use `composer update` and `composer dump-autoload` when is needed!

Usage
-----

[](#usage)

All you need to do is creating a `ResourceTable` with prepared builder object. Then add columns and call `make()`. That's it!

Examples
--------

[](#examples)

### Example 1: Simple use

[](#example-1-simple-use)

```
$news = DB::table('news')
    ->select(['news.id', 'news.subject']);

echo ResourceTable::of($news)
    ->addColumn([
        'index' => 'id',
        'label' => 'ID',
        'sortable' => true,
    ])
    ->addColumn([
        'index' => 'subject',
        'label' => 'Subject',
        'sortable' => true,
    ])
    ->make();
```

### Example 2: Adding columns

[](#example-2-adding-columns)

```
$news = DB::table('news')
    ->select(['news.id', 'news.subject']);

echo ResourceTable::of($news)
    ->addColumn([
        'index' => 'id',
        'label' => 'ID',
        'sortable' => true,
    ])
    ->addColumn([
        'index' => 'subject',
        'label' => 'Subject',
        'sortable' => true,
    ])
    ->addColumn([
        'index' => 'operations',
        'label' => 'Operations',
        'sortable' => false,
        'renderer' => function ($row) {
            return 'Delete';
        }
    ])
    // Or you can use a string which is a name of renderer class
    // NOTE: all renderer objects must have callable "render" method - an $row stdClass will be given with all row data
    ->addColumn([
        'index' => 'operations',
        'label' => 'Operations',
        'sortable' => false,
        'renderer' => 'Your\Full\Namespace\To\Renderer',
    ])
    ->make();
```

### Example 3: Joining tables

[](#example-3-joining-tables)

```
$news = DB::table('news')
    ->select(['news.id', 'news.subject', 'categories.name AS category_name'])
    ->leftJoin('categories', 'news.category_id', '=', 'categories.id');

return ResourceTable::of($news)
    ->addColumn([
        'index' => 'id',
        'label' => 'ID',
        'sortable' => true,
    ])
    ->addColumn([
        'index' => 'category_name',
        'label' => 'Category',
        'sortable' => true,
    ])
    ->addColumn([
        'index' => 'subject',
        'label' => 'Subject',
        'sortable' => true,
    ])
    ->addColumn([
        'index' => 'operations',
        'label' => 'Operations',
        'sortable' => false,
        'renderer' => function ($row) {
            return 'Delete';
        }
    ])
    ->make();
```

### Example 4: Set custom conditions

[](#example-4-set-custom-conditions)

```
$news = DB::table('news')
    ->select(['news.id', 'news.subject']);

echo ResourceTable::of($news)
    ->addColumn([
        'index' => 'id',
        'label' => 'ID',
        'sortable' => true,
    ])
    ->addColumn([
        'index' => 'subject',
        'label' => 'Subject',
        'sortable' => true,
    ])
    ->perPage(20)
    ->page(2)
    ->paginate(true)
    ->sort('id', 'DESC')
    ->filter(true)
    ->customView('my.custom.view.name')
    ->make();
```

Where `perPage(20)` sets resources per page. Method `page(2)` sets current page. Method `orderBy('id', 'DESC')` sets default sorting.

### Example 5: Setting up the searchable columns

[](#example-5-setting-up-the-searchable-columns)

Let's say your news can be `event` or `hot_topic` type.

```
$news = DB::table('news')
    ->select(['news.id', 'news.subject', 'news.type']);

echo ResourceTable::of($news)
    ->addColumn([
        'index' => 'id',
        'label' => 'ID',
        'sortable' => true,
    ])
    ->addColumn([
        'index' => 'subject',
        'label' => 'Subject',
        'sortable' => true,
        'searchable' => true,
    ])
    ->addColumn([
        'index' => 'type',
        'label' => 'Type',
        'sortable' => true,
        'searchable' => true,
        'type' => 'select',
        'options' => [
            'event' => 'Event',
            'hot_topic' => 'Hot topic',
        ]
    ])
    ->make();
```

### Example 6: Custom filter logic

[](#example-6-custom-filter-logic)

```
$news = DB::table('news')
    ->select(['news.subject']);

echo ResourceTable::of($news)
    ->addColumn([
        'index' => 'subject',
        'label' => 'Subject',
        'sortable' => true,
        'searchable' => true,
        'filter' => function($value) {
            // Do whatever you want with given value!
            return trim(mb_strtolower(($value));
        },

        // You can specify column name to search
        'filter_column' => 'subject_alias',
    ])
    ->make();
```

Resource Table will generate a `thead` tag with two rows. First will contain standard `th` columns but the second one will contain text inputs or select fields *(depends on column configuration)*. Currently Resource Table version supports following column types:

- string - script will be looking for value matching pattern `index LIKE '%value%'` *(used by default)*
- select - script will be looking for value matching pattern `index = 'value'`

#### Note

[](#note)

Resource Table will automatically inject `All` option with `_all` key to your all select type columns.

Creating pagination presenters
------------------------------

[](#creating-pagination-presenters)

Let's say you don't want to use default built-in Bootstrap 3 pagination HTML structure for your pagination. With ResourceTable you can create your own one or use built-in `AdminLTEPresenter`. So if you'are using Admin LTE Admin Theme you don't have to worry about pagination HTML! By default ResourceTable use Bootstrap 3 presenter which is default for Laravel 5.

### Using Admin LTE pagination presenter

[](#using-admin-lte-pagination-presenter)

```
$collection = ResourceTable::of($news)
    ->addColumn...
    ...
    ->setPaginationPresenter('Msieprawski\ResourceTable\Presenters\AdminLTEPresenter');
```

#### Note

[](#note-1)

Remember to use full path to class!

### Creating your own pagination presenter

[](#creating-your-own-pagination-presenter)

You can create your pagination presenter wherever you want but it's recommended to create `Presenters` directory under your `app` directory. For this example I created `MyCustomPresenter` under `app/Presenters` directory:

```
