2017 © Pedro Peláez
 

library eloquenttable

An HTML table generator for laravel collections

image

stevebauman/eloquenttable

An HTML table generator for laravel collections

  • PHP
  • 5 Dependents
  • 0 Suggesters
  • 15 Forks
  • 5 Open issues
  • 15 Versions
  • 5 % Grown

The README.md

Eloquent Table

Travis CI Code Climate Scrutinizer Code Quality Latest Stable Version Total Downloads License, (*1)

🚨 Abandoned 🚨

I'd heavily recommend the use of Orchestra/HTML over this package, as their solution is further tested, complete, and much more robust. This package will receive no further updates., (*2)

If you would like to be a maintainer of this package, please contact me at steven_bauman@outlook.com, or create an issue., (*3)

Thank you!, (*4)

Description

Eloquent table is an HTML table generator for laravel collections., (*5)

Installation

Include the package in composer.json:, (*6)

"stevebauman/eloquenttable": "1.1.*"

Now perform a composer update., (*7)

Laravel 4

Include the service providers in the bottom app/config/app.php config file:, (*8)

'Stevebauman\EloquentTable\PaginationServiceProvider',
'Stevebauman\EloquentTable\EloquentTableServiceProvider',

Publish the config file (optional), (*9)

php artisan config:publish stevebauman/eloquenttable

Laravel 5

Include the service providers in the bottom config/app.php config file:, (*10)

'Stevebauman\EloquentTable\EloquentTableServiceProvider',

Publish the config file (mandatory in Laravel 5), (*11)

php artisan vendor:publish

You're good to go!, (*12)

Note: The showPages() method below in unavailable in Laravel 5 due to the pagination changes. You'll need to display your pages manually using the render() method shown here: http://laravel.com/docs/5.0/pagination#usage, (*13)

Usage

Insert the trait on your model:, (*14)

class Book extends Eloquent {

    use \Stevebauman\EloquentTable\TableTrait;

    protected $table = 'books';

}

Grab records from your model like usual:, (*15)

$books = Books::get();

return view('books.index', compact('books'));

Inside your blade view, we just specify the columns we want to show, and then call the render method:, (*16)

{!!
    $books->columns(array(
        'id' => 'ID',
        'title' => 'Title',
        'author' => 'Authored By'
    ))
    ->render() 
!!}
Handling relationship values using means($column, $relationship):
{!!
    $books->columns(array(
        'id' => 'ID',
        'title' => 'Title',
        'author' => 'Authored By',
        'owned_by' => 'Owned By',
    ))
    ->means('owned_by', 'user.first_name')
    ->render()
!!}

The model books, needs to have a user method defining it's relation for this to work., (*17)

You must also use 'dot' notation to indicate the relationship., (*18)

Customizing the display of the column value using modify($column, $closure):
{!!
    $books->columns(array(
        'id' => 'ID',
        'title' => 'Title',
        'author' => 'Authored By',
        'owned_by' => 'Owned By',
    ))
    ->means('owned_by', 'user')
    ->modify('owned_by', function($user, $book) {
        return $user->first_name . ' ' . $user->last_name;
    })
    ->render() 
!!}

Using modify, we can specify the column we want to modify, and the function will return the current relationship record (if the column is a relationship), as well as the current base record, in this case the book., (*19)

Customizing the attributes of each cell of a column using modifyCell($column, $closure):
{!!
    $books->columns(array(
        'id' => 'ID',
        'title' => 'Title',
        'author' => 'Authored By',
        'owned_by' => 'Owned By',
    ))
    ->means('owned_by', 'user')
    ->modifyCell('owned_by', function($user) {
        return array('class' => $user->role);
    })
    ->render() 
!!}

Using modifyCell, we can specify the column of the cell we want to modify, and the function should return an array of attributes to be added to the cell., (*20)

Customizing the attributes of each row in the table using modifyRow($name, $closure):
{!!
    $books->columns(array(
        'id' => 'ID',
        'title' => 'Title',
        'author' => 'Authored By',
        'owned_by' => 'Owned By',
    ))
    ->means('owned_by', 'user')
    ->modifyRow('mod1', function($user) {
        return array('id' => 'user-'.$user->id);
    })
    ->render() 
!!}

Using modifyRow, we can add named modifications ('mod1' in our previous example), and the function should return an array of attributes to be added to each row., (*21)

In your controller:, (*22)

$books = Book::sort(Input::get('field'), Input::get('sort'))->get();

In your view:, (*23)

{!!
    $books->columns(array(
        'id' => 'ID',
        'title' => 'Title',
        'author' => 'Authored By',
        'owned_by' => 'Owned By',
    ))
    ->sortable(array('id', 'title'))
    ->render()
!!}

A link will be generated inside the column header that will be clickable. The HTML generated will look like:, (*24)

<a class="link-sort" href="http://www.example.com/books?field=id&amp;sort=desc">
    ID <i class="fa fa-sort"></i>
</a>
What about if we want to combine this all together, with pagination and sorting? Easy:

In your controller:, (*25)

$books = Book::sort(Input::get('field'), Input::get('sort'))->paginate(25);

return view('books.index', compact('books'));

In your view:, (*26)

{!!
    $books->columns(array(
        'id' => 'ID',
        'title' => 'Title',
        'author' => 'Authored By',
        'owned_by' => 'Owned By',
        'publisher' => 'Publisher',
    ))
    ->means('owned_by', 'user')
    ->modify('owned_by', function($user, $book) {
        return $user->first_name . ' ' . $user->last_name;
    })
    ->means('publisher', 'publisher')
    ->modify('publisher', function($publisher, $book) {
        return 'The publisher of this book: '. $publisher->name;
    })
    ->sortable(array('id', 'title'))
    ->showPages()
    ->render()
!!}
What if I want to generate a table for a relationship?:

In your controller:, (*27)

$book = Book::with('authors')->find(1);

return view('book.show', compact('book'));

In this case, the book is going to have many authors (hasMany relationship), (*28)

In your view:, (*29)

{!!
    $book->authors->columns(
        'id' => 'ID',
        'name' => 'Name',
        'books' => 'Total # of Books'
    )
    ->means('books', 'num_of_books')
    ->render()
!!}

Keep in mind, we cannot paginate the table, or provide sortable columns on relationships. If you need this, grab it separately:, (*30)

In your controller:, (*31)

$book = Book::find(1);

$authors = Authors::where('book_id', $book->id)->paginate(25);

return view('books.show', array(
    'book' => $book,
    'authors' => $authors,
));

In your view:, (*32)

{!!
    $authors->columns(array(
        'name' => 'Name',
    ))->render()
!!}
Customizing table attributes using attributes($attributes = array())
{!!
    $authors->columns(array(
        'name' => 'Name',
    ))
    ->attributes(array(
        'id' => 'table-1',
        'class' => 'table table-striped table-bordered',
    ))
    ->render()
!!}
Showing your pages somewhere else:

Just don't call the showPages() method on the collection and put your pages somewhere on your page like you would regularly do., (*33)

{!!
    $authors->columns(array(
        'name' => 'Name',
    ))
    ->attributes(array(
        'id' => 'table-1',
        'class' => 'table table-striped table-bordered',
    ))
    ->render()
!!}

<div class="text-center">{!! $authors->appends(Input::except('page'))->links() !!}</div>
Why is a pagination service provider required?

When calling paginate() on your models and/or collections, a different collection instance is returned. Unfortunately the only solution is to override the default paginator instance. However, this paginator extends laravel's built in paginator, so absolutely no functionality is removed or lost., (*34)

The Versions

28/03 2017

dev-master

9999999-dev

An HTML table generator for laravel collections

  Sources   Download

MIT

The Requires

 

by Steve Bauman

laravel php eloquent html table

28/03 2017

v1.1.7

1.1.7.0

An HTML table generator for laravel collections

  Sources   Download

MIT

The Requires

 

by Steve Bauman

laravel php eloquent html table

11/05 2016

v1.1.6

1.1.6.0

An HTML table generator for laravel collections

  Sources   Download

MIT

The Requires

 

by Steve Bauman

laravel php eloquent html table

01/06 2015

v1.1.5

1.1.5.0

An HTML table generator for laravel collections

  Sources   Download

MIT

The Requires

 

by Steve Bauman

laravel php eloquent html table

21/05 2015

v1.1.4

1.1.4.0

An HTML table generator for laravel collections

  Sources   Download

MIT

The Requires

 

by Steve Bauman

laravel php eloquent html table

09/05 2015

v1.1.3

1.1.3.0

An HTML table generator for laravel collections

  Sources   Download

MIT

The Requires

 

by Steve Bauman

laravel php eloquent html table

09/04 2015

v1.1.2

1.1.2.0

An HTML table generator for laravel collections

  Sources   Download

MIT

The Requires

 

by Steve Bauman

laravel php eloquent html table

07/04 2015

v1.1.1

1.1.1.0

An HTML table generator for laravel collections

  Sources   Download

MIT

The Requires

 

by Steve Bauman

laravel php eloquent html table

07/04 2015

v1.1.0

1.1.0.0

An HTML table generator for laravel collections

  Sources   Download

MIT

The Requires

 

by Steve Bauman

laravel php eloquent html table

23/02 2015

v1.0.2

1.0.2.0

An HTML table generator for laravel collections

  Sources   Download

MIT

The Requires

 

by Steve Bauman

laravel php eloquent html table

12/01 2015

v1.0.1

1.0.1.0

An HTML table generator for laravel collections

  Sources   Download

MIT

The Requires

 

by Steve Bauman

laravel php eloquent html table

12/01 2015

1.0.1

1.0.1.0

An HTML table generator for laravel collections

  Sources   Download

MIT

The Requires

 

by Steve Bauman

laravel php eloquent html table

30/11 2014

dev-1.x-stable

dev-1.x-stable

An HTML table generator for laravel collections

  Sources   Download

MIT

The Requires

 

by Steve Bauman

laravel php eloquent html table

30/11 2014

v1.0.0

1.0.0.0

An HTML table generator for laravel collections

  Sources   Download

MIT

The Requires

 

by Steve Bauman

laravel php eloquent html table

27/11 2014

1.0

1.0.0.0

An HTML table generator for laravel collections

  Sources   Download

MIT

The Requires

 

by Steve Bauman

laravel php eloquent html table