'Laravel console table with column width
I have just discovered the table rendering function in Laravel console command.
My project is built on Laravel 5.8, but even if there is an answer for more advanced versions it will be great to know.
I am looking for rendering a table with determined (or even better - limitted) column width. Something like:
$data = [
['John', 'The Curious Incident of the Dog in the Night-Time'],
['Rachel', 'The Catcher in the Rye']
];
// Code for columns width is missing
$this->table(['Name', 'Favorite book'], $data);
So the output will be:
+--------+-------------------------+
| Name | Favorite book |
+--------+-------------------------+
| John | The Curious Incident... |
| Rachel | The Catcher in the Rye |
+--------+-------------------------+
I saw that Symfony table (which laravel table is based on) has a setColumnWidths() function, but I cannot find any way to use it from the console command.
Does somebody know if it is possible?
Solution 1:[1]
Unfortunately you cannot use setColumnWidths, because the table only supports table style and column style, as you can see here.
You can directly use the Symfony table, or simply create your word limit (via Str::limit) :
$size = 20;
$data = [
['John', Str::limit('The Curious Incident of the Dog in the Night-Time', $size)],
['Rachel', Str::limit('The Catcher in the Rye', $size)]
];
$this->table(['Name', 'Favorite book'], $data);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Wahyu Kristianto |
