phplucidframe/console-table
Lightweight PHP library to render formatted ASCII tables in the console. Define headers, rows, alignment, padding, and borders; supports styling and flexible layouts for CLI apps, scripts, and Symfony/Laravel-style console output.
Installation
composer require phplucidframe/console-table
No additional configuration is required—just autoload via Composer.
First Use Case Display a simple table of Eloquent model data in Tinker or a command:
use PhpLucidFrame\ConsoleTable\ConsoleTable;
use App\Models\User;
$users = User::limit(10)->get(['id', 'name', 'email']);
$table = new ConsoleTable($users);
// Basic output
$table->display();
Where to Look First
README.md in the repo).src/ConsoleTable.php for core methods like display(), setHeaders(), and setRows().ConsoleTable usage in Laravel’s app/Console/Commands or tests.$table = new ConsoleTable([
['ID', 'Name', 'Email'],
[1, 'John Doe', 'john@example.com'],
[2, 'Jane Smith', 'jane@example.com'],
]);
$table->display();
$table = new ConsoleTable();
$table->setHeaders(['ID', 'Name', 'Email']);
$table->setRows(User::all()->map(fn ($user) => [$user->id, $user->name, $user->email])->toArray());
$table->display();
$table = new ConsoleTable($users);
$table->setStyle('border', true); // Add borders
$table->setStyle('padding', 1); // Adjust padding
$table->setStyle('color', ['header' => 'green', 'row' => 'white']);
$table->display();
use Illuminate\Console\Command;
class UserListCommand extends Command
{
protected $signature = 'users:list';
public function handle()
{
$table = new ConsoleTable(User::all());
$this->output->writeln($table->render());
}
}
$table = new ConsoleTable(User::paginate(10)->items());
$table->display();
// Manually add pagination info below the table
$this->info("Showing {$users->perPage()} of {$users->total()} entries.");
$table = new ConsoleTable(User::all());
$csvData = $table->toCsv();
file_put_contents('users.csv', $csvData);
Leverage Laravel’s Service Container Bind the table class for dependency injection:
$this->app->bind(ConsoleTable::class, function () {
return new ConsoleTable();
});
Reusable Table Builders Create a helper method in a service class:
class TableHelper
{
public static function buildUserTable(array $users, string $title = null)
{
$table = new ConsoleTable($users);
if ($title) $table->setTitle($title);
return $table;
}
}
Dynamic Column Selection
Use Laravel’s select() to limit columns before passing to ConsoleTable:
$table = new ConsoleTable(User::select('id', 'name', 'created_at')->get());
Localization Translate headers dynamically:
$table->setHeaders([
__('ID'),
__('Name'),
__('Email Address'),
]);
Data Structure Assumptions
map() to normalize:
$rows = User::get()->map(fn ($user) => [$user->id, $user->name])->toArray();
Styling Conflicts
$table->setStyle('reset', true);
Performance with Large Datasets
User::chunk(100, function ($users) {
$table = new ConsoleTable($users);
$table->display();
});
Hidden Dependencies
toCsv()) may require League\Csv or similar.Inspect Rows Before Rendering
$rows = $table->getRows();
dd($rows); // Debug data structure
Disable Styling for Debugging
$table->setStyle('disable', true); // Output raw data
Check for Encoding Issues
$rows = array_map('utf8_encode', $rows); // If needed
Custom Renderers Extend the base class to add features:
class ExtendedTable extends ConsoleTable
{
public function addFooter(array $data)
{
$this->footer = $data;
}
protected function renderFooter()
{
// Custom footer logic
}
}
Event Hooks
Override methods like render() or display() to inject logic:
$table = new ConsoleTable($users);
$table->display = function () use ($table) {
$this->info('Pre-render hook');
$table->render();
$this->comment('Post-render hook');
};
Plugin System Use Laravel’s service providers to register table modifiers:
$this->app->afterResolving(ConsoleTable::class, function ($table) {
$table->setStyle('border', true);
});
Integration with Laravel Debugbar Display tables in the debug toolbar:
if (app()->bound('debugbar')) {
\Debugbar::info($table->toArray());
}
How can I help you explore Laravel packages today?