$ this-> table()抛出[InvalidArgumentException]一行必须是数组或TableSeparator实例

Luk*_*uke 6 command-line symfony laravel laravel-4

我写了一个L4 Command类,但table输出抛出异常.

<?php

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

class Table extends Command {

    protected $name = 'table';

    public function fire()
    {
        //output table;
        $header = ['Name', 'Email', 'Age'];
        $row = ['Luke', 'me@email.uk', '99'];

        $this->info(sprintf("is array ? %s", is_array($row) ? 'true' : 'false'));
        //outputs is array ? true
        $this->table($header, $row);
        //throws exception
        // [InvalidArgumentException]
        // A row must be an array or a TableSeparator instance.

    }
}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

Kas*_*ars 8

您必须传入一组行.根据table定义:

void table(array $headers, array $rows, string $style = 'default')
Run Code Online (Sandbox Code Playgroud)

所以你要么做

$row = [['Luke', 'me@email.uk', '99']]; // An array of arrays, containing one row
Run Code Online (Sandbox Code Playgroud)

要么

$this->table($header, [$row]);
Run Code Online (Sandbox Code Playgroud)

  • 因为错误是通过addRow方法抛出的。只有一个标头,但有多行。检查日志中是否有完整的堆栈跟踪。 (2认同)