Installation:
composer require gbrock/laravel-table:dev-master
Register the service provider and facade in config/app.php:
'providers' => [
Gbrock\Table\Providers\TableServiceProvider::class,
],
'aliases' => [
'Table' => Gbrock\Table\Facades\Table::class,
]
Publish assets:
php artisan vendor:publish --provider="Gbrock\Table\Providers\TableServiceProvider"
First Use Case: In a controller, create a table from an Eloquent collection:
use Gbrock\Table\Facades\Table;
public function index()
{
$users = User::all();
$table = Table::create($users);
return view('users.index', compact('table'));
}
Render in Blade:
Use the provided Blade directive @table in your view:
@table(['table' => $table])
Basic Table Rendering:
$table = Table::create(User::paginate(10));
Automatically handles pagination via paginate() or cursor().
Customizing Columns: Define columns in the table constructor or via a closure:
$table = Table::create(User::all())
->columns([
'id' => 'ID',
'name' => 'Name',
'email' => 'Email',
'created_at' => function ($value) {
return $value->diffForHumans();
}
]);
Sortable Columns: Enable sorting for specific columns:
$table = Table::create(User::orderBy('name')->get())
->sortable(['name', 'email']);
Blade will render clickable headers for sorting.
Conditional Cell Data: Use closures to dynamically format cells:
$table = Table::create(User::all())
->column('active', function ($value, $row) {
return $value ? '<span class="label label-success">Active</span>' : '<span class="label label-danger">Inactive</span>';
});
Integration with Controllers/Repositories: Encapsulate table logic in a repository or service:
class UserTableService
{
public function build(UserRepository $users)
{
return Table::create($users->all())
->columns(['id', 'name', 'email'])
->sortable(['name', 'email']);
}
}
Pagination Control: Pass paginated results directly:
$table = Table::create(User::paginate(25));
Or manually set pagination:
$table = Table::create(User::all())
->paginate(25);
Reusing Table Configurations:
Use the TableBuilder facade to define reusable table configurations:
Table::builder('users')
->columns(['id', 'name', 'email'])
->sortable(['name', 'email'])
->paginate(25);
Then fetch it in a controller:
$table = Table::get('users', User::all());
Deprecated Features:
Pagination Quirks:
paginate(), ensure your query returns the correct number of results. The package does not modify the underlying query; it only renders pagination controls.simplePaginate()) may not work as expected. Stick to paginate() or manual pagination.Sorting Limitations:
$table = Table::create(User::orderBy(request('sort', 'name'))->get())
->sortable(['name', 'email']);
Then handle the sort parameter in your controller.Blade Directive Scope:
The @table directive expects a variable named table. If you pass the table under a different name (e.g., $dataTable), update the directive:
@table(['table' => $dataTable])
CSS/JS Dependencies:
The package assumes Bootstrap 3 for styling. If using a different framework (e.g., Tailwind), override the published views (resources/views/vendor/table/).
Inspect Table Structure: Dump the table object to understand its configuration:
dd($table->getColumns(), $table->getSortableColumns());
Check Published Views:
Override the default views in resources/views/vendor/table/ to customize rendering:
php artisan vendor:publish --tag=views
Handle Missing Columns: If a column doesn’t render, verify:
columns() array.Sorting Debugging: Add logging to track sorting parameters:
\Log::info('Sort param:', request('sort'));
Custom Cell Renderers:
Extend the Gbrock\Table\Cell class to create reusable renderers:
class StatusCell extends Gbrock\Table\Cell
{
public function render($value, $row)
{
return $value ? 'Active' : 'Inactive';
}
}
Use it in your table:
$table->column('active', new StatusCell());
Override Default Views: Publish and modify the Blade views for full control over HTML output:
php artisan vendor:publish --tag=views
Add Custom Actions: Although batch actions are not implemented, you can manually add them to the table footer:
@table(['table' => $table])
<div class="table-footer">
<button class="btn btn-danger">Delete Selected</button>
</div>
Integrate with Laravel Mix: If using Laravel Mix, ensure the package’s assets (if any) are compiled. The package is lightweight, but you may need to manually include Bootstrap JS/CSS if not already loaded.
$table = Table::create(User::select('id', 'name', 'email')->get());
cursor() instead of get() to reduce memory usage:
$table = Table::create(User::cursor());
How can I help you explore Laravel packages today?