2017 © Pedro Peláez
 

library laravel-table

Table functionality for Laravel models

image

brynj-digital/laravel-table

Table functionality for Laravel models

  • Tuesday, March 27, 2018
  • by brynj-digital
  • Repository
  • 1 Watchers
  • 3 Stars
  • 122 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 19 Forks
  • 0 Open issues
  • 13 Versions
  • 149 % Grown

The README.md

Laravel Tables

Made for Laravel 5 Latest Tag , (*1)

This package contains flexible ways of rendering Eloquent collections as dynamic HTML tables. This includes techniques for sortable columns, customizable cell data, automatic pagination, ~~user-definable rows-per-page, batch action handling, and extensible filtering~~ (coming soon)., (*2)

Installation

Require the package in your composer.json:, (*3)

"gbrock/laravel-table": "dev-master"

Add the service provider to config/app.php and, optionally, the Facade:, (*4)

'Gbrock\Table\Providers\TableServiceProvider',
...
'Table'      => 'Gbrock\Table\Facades\Table',

Publish the views and config:, (*5)

php artisan vendor:publish

Usage

In order to render an HTML table of Eloquent models into a view, first create a Table object, passing in your model collection (this could be done in your controller, repository, or any service class):, (*6)

php $rows = User::get(); // Get all users from the database $table = Table::create($rows); // Generate a Table based on these "rows", (*7)

Then pass that object to your view:, (*8)

return view('users.index', ['table' => $table]);

In your view, the table object can be rendered using its render function:, (*9)

{!! $table->render() !!}

Which would render something like this:, (*10)

Basic example, (*11)

Sorting

To add links in your headers which sort the indicated column, add the Sortable trait to your model. Since no fields are allowed to be sorted by default (for security reasons), also add a sortable array containing allowed fields., (*12)

use Gbrock\Table\Traits\Sortable;

class User extends Model {

    use Sortable;

    /**
     * The attributes which may be used for sorting dynamically.
     *
     * @var array
     */
    protected $sortable = ['username', 'email', 'created_at'];

This adds the sortable scope to your model, which you should use when retrieving rows. Altering our example, $rows = User::get() becomes:, (*13)

```php $rows = User::sorted()->get(); // Get all users from the database, but listen to the user Request and sort accordingly, (*14)


Now, our table will be rendered with links in the header: ![Sortable example](https://raw.githubusercontent.com/gbrock/laravel-table/master/examples/images/sortable_initialization.png) The links will contain query strings like `?sort=username&direction=asc`. ### Pagination If you paginate your Eloquent collection, it will automatically be rendered below the table: ```php $rows = User::sorted()->paginate(); // Get all users from the database, sort, and paginate

Customization

Columns

Pass in a second argument to your database call / Table creation, columns:, (*15)

 $table = Table::create($rows, ['username', 'created_at']); // Generate a Table based on these "rows"

Cells

You can specify a closure to use when rendering cell data when adding the column:, (*16)

// We pass in the field, label, and a callback accepting the model data of the row it's currently rendering
$table->addColumn('created_at', 'Added', function($model) {
    return $model->created_at->diffForHumans();
});

Also, since the table is accessing our model's attributes, we can add or modify any column key we'd like by using accessors:, (*17)

    protected function getRenderedCreatedAtAttribute()
    {
        // We access the following diff string with "$model->rendered_created_at"
        return $this->created_at->diffForHumans();
    }

The default view favors the rendered_foobar attribute, if present, otherwise it uses the foobar attribute., (*18)

View

A copy of the view file is located in /resources/vendor/gbrock/tables/ after you've run php artisan vendor:publish. You can copy this file wherever you'd like and alter it, then tell your table to use the new view:, (*19)

$table->setView('users.table');

The Versions

27/03 2018

dev-master

9999999-dev

Table functionality for Laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel model html table filtering sorting

25/10 2015

dev-rewrite-with-tests

dev-rewrite-with-tests

Table functionality for Laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model html table

16/10 2015

0.3.4

0.3.4.0

Table functionality for Laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel model html table filtering sorting

14/10 2015

0.3.3

0.3.3.0

Table functionality for Laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel model html table filtering sorting

12/10 2015

0.3.2

0.3.2.0

Table functionality for Laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel model html table filtering sorting

12/10 2015

0.3.1

0.3.1.0

Table functionality for Laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel model html table filtering sorting

12/10 2015

0.3.0

0.3.0.0

Table functionality for Laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel model html table filtering sorting

14/05 2015

0.2.4

0.2.4.0

Table functionality for Laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel model html table filtering sorting

16/04 2015

0.2.3

0.2.3.0

Table functionality for Laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel model html table filtering sorting

16/04 2015

0.2.2

0.2.2.0

Table functionality for Laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel model html table filtering sorting

30/03 2015

0.2.1

0.2.1.0

Table functionality for Laravel models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel model html table filtering sorting

29/03 2015

0.2

0.2.0.0

Table functionality for Laravel models

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

laravel model html table filtering sorting

23/03 2015

0.1-alpha

0.1.0.0-alpha

Table functionality for Laravel models

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

laravel model html table filtering sorting