yeejiawei/laravel-table-generator
Installation:
composer require yeejiawei/laravel-table-generator
Publish the config (if needed) with:
php artisan vendor:publish --provider="YeeJiaWei\LaravelTableGenerator\TableGeneratorServiceProvider"
First Use Case:
Generate a basic table for a User model with pagination:
use YeeJiaWei\LaravelTableGenerator\Facades\TableGenerator;
$users = User::paginate(10);
return TableGenerator::create($users)
->setTableName('Users')
->addColumn('id')
->addColumn('name')
->addColumn('email')
->render();
Where to Look First:
config/table-generator.php for customizable defaults (e.g., column types, styling).TableGenerator facade methods in src/Facades/TableGenerator.php for available chainable methods.Data Binding:
Pass a Laravel collection or paginator (e.g., Model::paginate()) to TableGenerator::create($data).
Column Configuration: Use fluent methods to define columns:
->addColumn('id', ['sortable' => true])
->addColumn('name', ['searchable' => true, 'width' => '20%'])
->addColumn('status', ['type' => 'select', 'options' => ['active' => 'Active', 'inactive' => 'Inactive']])
Action Buttons: Attach CRUD actions with route names or URLs:
->setCreatable('users.create')
->setViewable('users.show')
->setEditable('users.edit')
->setDeletable('users.destroy')
->setEnable('users.toggle-status')
Timestamps: Add created/updated columns with:
->addCreatedAtColumns(['label' => 'Created On'])
->addUpdatedAtColumns(['label' => 'Last Updated'])
Rendering: Output the table in a Blade view or return raw HTML:
// Blade view
@include('table-generator::table', ['table' => $table])
// Raw HTML (e.g., for APIs)
echo $table->render();
Blade Views:
Use the @include directive with the package's view path (table-generator::table).
Customize the view by publishing it first:
php artisan vendor:publish --tag=table-generator-views
API Responses: For APIs, return the rendered HTML or a structured array:
return response()->json(['table' => $table->toArray()]);
Dynamic Columns:
Fetch columns dynamically from a model's $fillable or accessors:
$columns = collect($model->fillable)->map(fn($field) => $field)->toArray();
$table->addColumns($columns);
Styling: Override CSS/JS by publishing assets:
php artisan vendor:publish --tag=table-generator-assets
Then extend the default styles in your app's assets.
Localization: Customize labels/buttons via the config:
'buttons' => [
'create' => __('Create New'),
'edit' => __('Edit'),
// ...
],
Archived Package:
Pagination Issues:
Model::paginate()), not a raw collection, for pagination controls to work.render() method.Column Type Mismatches:
->addColumn('metadata', function($row) {
return json_encode($row->metadata);
})
Route Binding:
setEditable) expect route names, not URLs. Use route('users.edit', $user) in Blade if needed.CSRF Tokens:
@csrf.JavaScript Dependencies:
Inspect the Table Object: Dump the table configuration before rendering:
dd($table->getColumns(), $table->getOptions());
Check Published Views:
If the table doesn’t render, verify the published view (resources/views/vendor/table-generator/table.blade.php) exists and is correctly included.
Log Configuration:
Add debug logs to config/table-generator.php:
'debug' => env('TABLE_GENERATOR_DEBUG', false),
Then check Laravel logs for package activity.
Extend Functionality:
Create a custom table builder by extending the YeeJiaWei\LaravelTableGenerator\TableGenerator class:
namespace App\Services;
use YeeJiaWei\LaravelTableGenerator\TableGenerator as BaseTable;
class CustomTable extends BaseTable {
public function addCustomColumn($field) {
$this->addColumn($field, ['type' => 'custom']);
// Add logic here
}
}
Conditional Columns: Hide/show columns based on user roles:
->addColumn('admin_only_field', [
'visible' => function($row) {
return auth()->user()->isAdmin();
}
])
Bulk Actions: Add bulk select/destroy functionality by extending the table’s JavaScript:
// Publish assets first, then extend in your app.js
$(document).ready(function() {
$('.table-generator').on('bulkAction', function() {
// Custom logic
});
});
Performance:
->setPerPage(50) or implement server-side processing.Testing:
Mock the TableGenerator facade in tests:
$this->partialMock(TableGenerator::class, function($mock) {
$mock->shouldReceive('render')->andReturn('<table>...</table>');
});
Fallback for Missing Data: Handle null values in columns:
->addColumn('description', ['fallback' => 'N/A'])
How can I help you explore Laravel packages today?