voilab/tctable
Laravel package for building sortable, filterable, paginated tables from Eloquent data. Define columns and actions, render via views, and handle server-side searching and ordering. Useful for admin panels and data listings with minimal boilerplate.
## Getting Started
### Minimal Steps
1. **Installation**
```bash
composer require voilab/tctable tecnickcom/tcpdf
Register the service provider in config/app.php:
'providers' => [
Voilab\TcTable\TcTableServiceProvider::class,
],
First Use Case: Quick PDF Table
use Voilab\TcTable\Facades\TcTable;
$data = [
['id' => 1, 'name' => 'John Doe', 'email' => 'john@example.com'],
['id' => 2, 'name' => 'Jane Smith', 'email' => 'jane@example.com'],
];
$pdf = TcTable::make()
->setData($data)
->setHeaders(['ID', 'Name', 'Email'])
->renderAndDownload('users.pdf');
Where to Look First
TcTable for quick usage.config/tctable.php for defaults.vendor/voilab/tctable/README.md for advanced options.Data-Driven Tables
// Eloquent Collection
$table = TcTable::make()
->setData(User::all()->toArray())
->setHeaders(['ID', 'Name', 'Email']);
// Raw Query
$table->setData(DB::table('users')->get()->toArray());
Dynamic Styling
$table->setStyle([
'header' => ['font' => 'helvetica', 'size' => 10, 'bold' => true, 'bgcolor' => [200, 200, 200]],
'cell' => ['padding' => 3, 'border' => 0.3, 'align' => 'C'],
]);
Pagination Control
$table->setPageBreakOptions([
'maxRowsPerPage' => 25, // Force page break every 25 rows
'breakOn' => ['header', 'footer'], // Preserve headers/footers
'mode' => 'full', // Break entire table if needed
]);
Memory-Efficient Chunking
$table->setChunkSize(1000)
->setData(User::query()->cursor()); // Stream data
Blade Integration
// Controller
return view('report', [
'table' => TcTable::make()->setData($data)->setHeaders($headers),
]);
// Blade
{!! $table->render() !!}
Custom Cell Logic
$table->setCellCallback(function ($row, $column) {
if ($column === 'status') {
return $row['active'] ? '<span style="color:green">Active</span>' : '<span style="color:red">Inactive</span>';
}
return $row[$column];
});
Async Generation with Queues
// Job
class GeneratePdfJob implements ShouldQueue
{
public function handle()
{
$table = TcTable::make()
->setData($this->data)
->setHeaders($this->headers)
->renderAndStore('path/to/file.pdf');
}
}
// Dispatch
GeneratePdfJob::dispatch($data, $headers)->onQueue('pdfs');
Storage Integration
use Illuminate\Support\Facades\Storage;
$table->renderAndStore('reports/users.pdf', 'public');
$url = Storage::url('reports/users.pdf');
Memory Limits
cursor() or chunk():
$table->setChunkSize(500)->setData(User::query()->cursor());
memory_get_usage() in your job.TCPDF Configuration Conflicts
config/tctable.php:
'tcpdf' => [
'default_font' => 'dejavusans',
'default_font_size' => 8,
'margin' => [15, 15, 15, 15],
],
Page Break Anomalies
$table->setPageBreakOptions([
'breakOn' => ['header', 'footer'],
'mode' => 'full',
]);
Data Type Errors
if (!is_array($data)) {
$data = $data->toArray(); // For Laravel Collections
}
Styling Not Applying
$table->setStyle([
'header' => ['font' => 'helvetica', 'size' => 10, 'bold' => true],
'cell' => ['padding' => 4, 'border' => 0.5, 'align' => 'L'],
]);
Font Loading Failures
$pdf = $table->getPdf();
$pdf->AddFont('dejavusans', '', 'DejaVuSans.php');
Enable TCPDF Debugging
$pdf = $table->getPdf();
$pdf->SetPrintHeader(true);
$pdf->SetPrintFooter(true);
$pdf->SetDebug(true);
Log Table Data
\Log::debug('Table Data:', ['data' => $table->getData()]);
Check TCPDF Errors
try {
$table->render();
} catch (\Exception $e) {
\Log::error('TCPDF Error: ' . $e->getMessage());
// Fallback to alternative PDF generation
}
Lazy Loading
$table->setLazyLoad(true)->setData(User::query()->cursor());
Cache Generated PDFs
$pdfPath = cache()->remember("pdf_{$userId}", now()->addHours(1), function () use ($table) {
return $table->renderAndStore("user_{$userId}.pdf");
});
Queue Batch Processing
foreach ($userIds as $id) {
GeneratePdfJob::dispatch($id)->onQueue('pdfs');
}
Custom TCPDF Instance
$pdf = new \TCPDF();
$pdf->SetCreator('Your App');
$table = new \Voilab\TcTable\TcTable($pdf);
Override Rendering Logic
$table->setRenderer(function ($pdf, $data, $headers) {
// Custom rendering logic
$pdf->AddPage();
$pdf->writeHTML($this->generateCustomHtml($data, $headers));
});
Add Custom Headers/Footers
$table->setHeaderCallback(function ($pdf) {
$pdf->SetFont('helvetica', 'B', 12);
$pdf->Cell(0, 10, 'Custom Header', 0, false, 'C');
});
Integrate with Laravel Events
event(new PdfGenerated($pdfPath, $userId));
Default Values
config/tctable.php for:
Environment-Specific Settings
if (app()->environment('production')) {
$table->setChunkSize(2000); // Larger chunks in production
}
TCPDF Version Pinning
composer.json:
"require": {
"tecnickcom/tcpdf": "6.5.2"
}
$this->app->bind(TcTable::class, function ($app) {
$
How can I help you explore Laravel packages today?