0

我刚刚在Laravel 控制台命令中发现了表格渲染功能。
我的项目是基于 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);

所以输出将是:

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

我看到Symfony 表(laravel 表所基于)有一个setColumnWidths()功能,但我无法从控制台命令中找到任何使用它的方法。

有人知道这是否可能吗?

4

1 回答 1

1

不幸的是,您不能使用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);
于 2022-02-23T09:54:20.240 回答