stevebauman/eloquenttable
Abandoned package: an HTML table generator for Laravel Eloquent collections. Provides a TableTrait and service provider setup (Laravel 4/5) to render collection data as tables, with limited pagination support (Laravel 5 requires manual render()).
Installation:
composer require stevebauman/eloquenttable:1.1.*
For Laravel 5, add the service provider to config/app.php:
'Stevebauman\EloquentTable\EloquentTableServiceProvider',
Publish the config (optional but recommended for customization):
php artisan vendor:publish --provider="Stevebauman\EloquentTable\EloquentTableServiceProvider"
Apply the Trait:
Add TableTrait to your Eloquent model:
use Stevebauman\EloquentTable\TableTrait;
class Book extends Model {
use TableTrait;
}
First Use Case: In your controller, fetch records:
$books = Book::all();
return view('books.index', compact('books'));
In your Blade view, define columns and render:
{!! $books->columns(['id' => 'ID', 'title' => 'Title'])->render() !!}
Column Definition:
Use columns() to map model attributes to table headers:
$books->columns([
'id' => 'ID',
'title' => 'Title',
'author' => 'Authored By'
]);
Relationship Handling:
Use means() to specify relationship paths (dot notation):
$books->means('owned_by', 'user.first_name');
Customize relationship display with modify():
$books->modify('owned_by', function($user, $book) {
return $user->first_name . ' ' . $user->last_name;
});
Styling and Attributes:
$books->attributes(['class' => 'table table-striped']);
$books->modifyCell('owned_by', function($user) {
return ['class' => $user->role];
});
$books->modifyRow('user_row', function($book) {
return ['id' => 'book-' . $book->id];
});
Sorting:
$books = Book::orderBy(Input::get('field'), Input::get('sort'))->get();
$books->sortable(['id', 'title']);
Pagination:
showPages() (deprecated in L5):
$books->showPages();
{!! $books->appends(request()->query())->links() !!}
Relationship Tables:
For hasMany relationships, chain columns() directly:
$book->authors->columns(['name' => 'Name', 'email' => 'Email']);
render() in {!! !!} to output raw HTML.@component('components.table', ['items' => $books, 'columns' => ['id', 'title']])
@endcomponent
Laravel 5 Pagination:
showPages() is non-functional in Laravel 5. Use Laravel’s native links() method instead.{!! $books->appends(request()->except('page'))->links() !!}
Relationship Caching:
$books = Book::with('user', 'publisher')->get();
Sorting Icons:
fa-sort) require Font Awesome. Customize via config or override the sortIcon() method.Deprecated Methods:
showPages() in Laravel 5. Use render() + manual pagination links.HTML Escaping:
modify() closures may cause XSS. Use e() or htmlspecialchars() if needed.with()).means() paths (e.g., user.first_name vs. user.firstName).class="table").!important overrides).Input::get('field') and Input::get('sort').Custom Renderers:
Override the render() method in a child trait to modify HTML structure:
trait CustomTableTrait {
public function render() {
return '<div class="custom-table">' . parent::render() . '</div>';
}
}
Dynamic Column Logic:
Use modify() for conditional rendering:
$books->modify('status', function($status) {
return $status === 'active' ? '<span class="label label-success">Active</span>' : 'Inactive';
});
Event Hooks:
Extend the package by listening to Eloquent events (e.g., retrieved) to pre-process data before table generation.
Localization: Override column headers dynamically:
$books->modify('title', function($title) {
return trans('labels.book_title');
});
paginate().$cachedColumns = cache()->remember('book_table_columns', 60, function() {
return ['id', 'title', 'created_at'];
});
How can I help you explore Laravel packages today?