PHPackages                             bogdanpet/eloquent-datatable - 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. [Database &amp; ORM](/categories/database)
4. /
5. bogdanpet/eloquent-datatable

ActivePackage[Database &amp; ORM](/categories/database)

bogdanpet/eloquent-datatable
============================

1.0.3(8y ago)040MITPHPPHP &gt;=5.3.9

Since Apr 30Pushed 8y agoCompare

[ Source](https://github.com/bogdanpet/eloquent-datatable)[ Packagist](https://packagist.org/packages/bogdanpet/eloquent-datatable)[ RSS](/packages/bogdanpet-eloquent-datatable/feed)WikiDiscussions develop Synced yesterday

READMEChangelog (4)DependenciesVersions (4)Used By (0)

Description
-----------

[](#description)

PHP class for displaying Eloquent collections as HTML table. Tested in Laravel, but should work in any project using eloquent as standalone package.

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

[](#installation)

Download and require Datatable.php and DatatableActions.php. or install it via composer.

```
composer require bogdanpet/eloquent-datatable

```

Usage
-----

[](#usage)

#### Basic usage

[](#basic-usage)

For example, let's say that we have collection of users we want to show as a table. Creating collection and sharing it to your view should look like this in Laravel.

```
public function showUsers() {
    $users = User::all();

    return view('users', compact('users'));
}

```

Now, if you are intending to show this collection as HTML table you can do this simply with Datatable class. You can either create new instance or take advantage of laravel's dependency injection.

```
use Bogdanpet\Datatables\Datatable;

...

public function showUsers(Datatable $datatable) {
    $users = User::all();

    $datatable->setData($users);
    $datatable->setColumns(['id, 'name', 'email']);

    return view('users', compact('datatable'));
}

```

Then, in your blade template, just put table where you want it using show() method.

```

    {!! $datatable->show(); !!}

```

If you need to customize table more, you can use separate methods instead of show() method.

```

    {!! $datatable->open(['class' => 'table-responsive']); // Generate opening  tag with optional attributes. !!}
    {!! $datatable->tableHead(); // Generate table column headings. !!}
    {!! $datatable->tableBody(); // Generate main table rows. !!}
    {!! $datatable->tableFoot(); // Generate table footer. !!}
    {!! $datatable->close(); // Generate closing  tag. !!}
    {!! $datatable->pagination(); // Generate pagination links if you are using User::paginate() to create collection !!}

```

#### Static usage

[](#static-usage)

For quick building of datatable in your view, if you already have access to collection, you can create table directly with static method make().

```

    {!! \Bogdanpet\Datatables\Datatable::make($users, ['id, 'name', 'email'] !!}

```

#### Datatable actions

[](#datatable-actions)

For generating action links I provided custom column 'actions'. For example, if you want to create links to edit and delete user from database, you can add 'actions' to columns and then set actions.

```
$datatable->setColumns(['id, 'name', 'email', 'actions']);
$datatable->setActions([
    [ 'Edit', '/user/edit/{id}', ['class' => 'table-action'] ],
    [ 'Delete', '/user/delete/{id}', ['class' => 'table-action'] ]
]);

```

As you can see, actions are set as array of arrays, each array contains link text as first item, route as second and optional third item which is array of link attributes. {id} is wildcard for laravel routing system, and will be replaced with corresponding user's id from the database. You can use wildcard for any database column like {name} or {email}. Example above will generate two links for each user in actions column.

```
Edit
Delete

```

#### Advanced usage

[](#advanced-usage)

For complete control over your datatable you can extend main Datatable class. For example, Let's create UsersDatatable class.

```
