Created
December 4, 2019 20:49
dump table in laravel phpunit or artisan tinker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console; | |
use Symfony\Component\Console\Helper\Table as SymfonyTable; | |
use Symfony\Component\Console\Output\StreamOutput; | |
class Table | |
{ | |
static function dump ($headers, $rows = null) { | |
if ($rows === null) { | |
$rows = $headers; | |
$headers = null; | |
} | |
if (!$rows || count($rows) === 0) { | |
dump('no table data'); | |
} | |
$output = new StreamOutput(fopen('php://stdout', 'w')); | |
$table = new SymfonyTable($output); | |
if ($headers === true) { | |
$table->setHeaders(array_keys($rows[0])); | |
} else if (is_array($headers) && count($headers) > 0) { | |
$table->setHeaders($headers); | |
} | |
$table->setRows($rows); | |
// writes to output | |
$table->render(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
php artisan tinker
example: