PHP code example of crumbls / laravel-cli-table

1. Go to this page and download the library: Download crumbls/laravel-cli-table library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

crumbls / laravel-cli-table example snippets


use Crumbls\LaravelCliTable\SelectableTable;

class YourCommand extends Command
{
    public function handle()
    {
        $table = new SelectableTable($this->output);
        
        $table->setHeaders(['ID', 'Name', 'Email']);
        $table->setRows([
            ['1', 'John Doe', '[email protected]'],
            ['2', 'Jane Smith', '[email protected]'],
            ['3', 'Bob Johnson', '[email protected]'],
        ]);

        // Interactive mode - returns selected row data
        $selectedRow = $table->selectRow();
        
        if ($selectedRow) {
            $this->info("Selected: " . $selectedRow[1]); // John Doe, Jane Smith, etc.
        }
    }
}

$selectedData = $table->selectRow(function($row, $index) {
    return [
        'index' => $index,
        'data' => $row,
        'id' => $row[0]
    ];
});

$table->setInteractive(false);
$table->render(); 

// Set both colors at once
$table->setSelectedColors('green', 'white');

// Or set them individually
$table->setSelectedBackgroundColor('magenta');
$table->setSelectedForegroundColor('yellow');

// Available colors: black, red, green, yellow, blue, magenta, cyan, white

class SelectUserCommand extends Command
{
    public function handle()
    {
        $users = User::all(['id', 'name', 'email', 'status']);
        
        $table = new SelectableTable($this->output);
        $table->setHeaders(['ID', 'Name', 'Email', 'Status'])
              ->setRows($users->toArray())
              ->setSelectedColors('blue', 'white');
        
        $selected = $table->selectRow(function($row, $index) {
            return User::find($row[0]);
        });
        
        if ($selected) {
            $this->info("Selected user: {$selected->name}");
        }
    }
}

use Symfony\Component\Console\Helper\TableCell;

$table->addRow(['1', 'John Doe', '[email protected]', 'Active']);
$table->addRow(['2', 'Jane Smith', '[email protected]', 'Active']);
$table->addRow([new TableCell('+ Add New User', ['colspan' => 4])]);

return [
    'instructions' => 'Use ↑/↓ arrows to navigate, Enter to select, q/Esc to exit',
    'selected_row' => 'Selected Row: :current/:total',
    'selection_cancelled' => 'Selection cancelled',
    // Add your own translations...
];
bash
php artisan vendor:publish --tag=cli-table-lang