繁体   English   中英

Laravel 带列宽的控制台表

[英]Laravel console table with column width

我刚刚在Laravel 控制台命令中发现了表渲染 function。
我的项目建立在 Laravel 5.8 上,但即使有更高级版本的答案,也很高兴知道。

我正在寻找呈现具有确定(甚至更好 - 有限)列宽的表格。 就像是:

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

所以 output 将是:

+--------+-------------------------+
| Name   | Favorite book           |
+--------+-------------------------+
| John   | The Curious Incident... |
| Rachel | The Catcher in the Rye  |
+--------+-------------------------+

我看到Symfony 表(laravel 表基于该表)有一个setColumnWidths() function,但我找不到任何方法从控制台命令使用它。

有人知道这是否可能吗?

不幸的是你不能使用setColumnWidths ,因为table只支持表格样式和列样式,正如你在这里看到的那样。

您可以直接使用 Symfony 表,或者简单地创建您的字数限制(通过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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM