Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Console Table Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require phplucidframe/console-table
    

    No additional configuration is required—just autoload via Composer.

  2. 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();
    
  3. Where to Look First

    • Documentation (if available; check README.md in the repo).
    • Source Code: Focus on src/ConsoleTable.php for core methods like display(), setHeaders(), and setRows().
    • Examples: Search for ConsoleTable usage in Laravel’s app/Console/Commands or tests.

Implementation Patterns

Common Workflows

1. Basic Table Rendering

$table = new ConsoleTable([
    ['ID', 'Name', 'Email'],
    [1, 'John Doe', 'john@example.com'],
    [2, 'Jane Smith', 'jane@example.com'],
]);
$table->display();

2. Dynamic Data from Eloquent

$table = new ConsoleTable();
$table->setHeaders(['ID', 'Name', 'Email']);
$table->setRows(User::all()->map(fn ($user) => [$user->id, $user->name, $user->email])->toArray());
$table->display();

3. Custom Styling

$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();

4. Integration with Artisan Commands

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());
    }
}

5. Pagination Support

$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.");

6. Exporting to CSV

$table = new ConsoleTable(User::all());
$csvData = $table->toCsv();
file_put_contents('users.csv', $csvData);

Integration Tips

  1. Leverage Laravel’s Service Container Bind the table class for dependency injection:

    $this->app->bind(ConsoleTable::class, function () {
        return new ConsoleTable();
    });
    
  2. 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;
        }
    }
    
  3. Dynamic Column Selection Use Laravel’s select() to limit columns before passing to ConsoleTable:

    $table = new ConsoleTable(User::select('id', 'name', 'created_at')->get());
    
  4. Localization Translate headers dynamically:

    $table->setHeaders([
        __('ID'),
        __('Name'),
        __('Email Address'),
    ]);
    

Gotchas and Tips

Pitfalls

  1. Data Structure Assumptions

    • The package expects 2D arrays (headers + rows) or Eloquent collections with consistent keys.
    • Fix: Ensure data is flattened or use map() to normalize:
      $rows = User::get()->map(fn ($user) => [$user->id, $user->name])->toArray();
      
  2. Styling Conflicts

    • Custom styles (e.g., colors) may clash with terminal themes or Laravel’s output formatting.
    • Fix: Test in your target environment or reset styles:
      $table->setStyle('reset', true);
      
  3. Performance with Large Datasets

    • Rendering thousands of rows may lag or crash.
    • Fix: Use pagination or chunking:
      User::chunk(100, function ($users) {
          $table = new ConsoleTable($users);
          $table->display();
      });
      
  4. Hidden Dependencies

    • Some methods (e.g., toCsv()) may require League\Csv or similar.
    • Fix: Check composer dependencies or implement a fallback.

Debugging Tips

  1. Inspect Rows Before Rendering

    $rows = $table->getRows();
    dd($rows); // Debug data structure
    
  2. Disable Styling for Debugging

    $table->setStyle('disable', true); // Output raw data
    
  3. Check for Encoding Issues

    • Non-ASCII characters (e.g., UTF-8) may break rendering.
    • Fix: Ensure your terminal supports UTF-8 or strip special chars:
      $rows = array_map('utf8_encode', $rows); // If needed
      

Extension Points

  1. 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
        }
    }
    
  2. 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');
    };
    
  3. Plugin System Use Laravel’s service providers to register table modifiers:

    $this->app->afterResolving(ConsoleTable::class, function ($table) {
        $table->setStyle('border', true);
    });
    
  4. Integration with Laravel Debugbar Display tables in the debug toolbar:

    if (app()->bound('debugbar')) {
        \Debugbar::info($table->toArray());
    }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
sentix/ai-chatbot
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky