Laravel的DD帮助函数是否正常工作?

4

我喜欢使用dd函数进行调试。这一次,当我用它显示约会列表时,我无法看到(没有可点击的箭头)属性和原始数据。相反,我看到了方括号[ …19]的显示,不确定为什么。

Collection {#3061 ▼
  #items: array:548 [▼
    0 => Appointment {#821 ▼
      #table: "appointments"
      #fillable: array:16 [ …16]
      #connection: null
      #primaryKey: "id"
      #perPage: 15
      +incrementing: true
      +timestamps: true
      #attributes: array:19 [ …19]
      #original: array:19 [ …19]
      #relations: array:2 [ …2]
      #hidden: []
      #visible: []
      #appends: []
      #guarded: array:1 [ …1]
      #dates: []
      #dateFormat: null
      #casts: []
      #touches: []
      #observables: []
      #with: []
      #morphClass: null
      +exists: true
      +wasRecentlyCreated: false
    }
    1 => Appointment {#822 ▶}
    2 => Appointment {#823 ▶}
    3 => Appointment {#824 ▶}
    4 => Appointment {#825 ▶}
    5 => Appointment {#826 ▶}
    6 => Appointment {#827 ▶}
    7 => Appointment {#828 ▶}

在列表中的后面,我甚至无法查看约会的详细信息(没有箭头):

    81 => Appointment {#902 ▶}
    82 => Appointment {#903 ▶}
    83 => Appointment {#904 ▶}
    84 => Appointment {#905 ▶}
    85 => Appointment {#906 …23}
    86 => Appointment {#907 …23}
    87 => Appointment {#908 …23}
    88 => Appointment {#909 …23}
    89 => Appointment {#910 …23}
    90 => Appointment {#911 …23}

但是当我使用 var_dump时,我的数据看起来很好:

    array(548) {
      [0]=>
      array(21) {
        ["id"]=>
        int(149)
        ["appointmenttype_id"]=>
        NULL
        ["appointmentlocationtype_id"]=>
        NULL
        ["appointment_start"]=>
        object(Carbon\Carbon)#812 (3) {
          ["date"]=>
          string(26) "2015-12-21 07:00:00.000000"
          ["timezone_type"]=>
          int(3)
          ["timezone"]=>
          string(16) "America/New_York"
        }
        ["appointment_end"]=>
        object(Carbon\Carbon)#811 (3) {
          ["date"]=>
          string(26) "2015-12-21 09:00:00.000000"
          ["timezone_type"]=>
          int(3)
          ["timezone"]=>
          string(16) "America/New_York"
        }

有其他人也遇到过这种情况吗?

3个回答

5
这是一个不太为人知的陷阱,当你返回的结果列表过大时会出现问题。一般来说,dd()旨在快速概览你返回的数据,并且在较小的数据组上很好地处理“钻取”操作。一旦你达到某个数量级(我忘记确切的数字了,也许是500?),该功能将不再起作用。
如果你绝对需要在代码中使用这些数据之前查看它们,请在get()结果之前使用limit()子句,或者使用dd($example[0])查看单个结果的详细信息。希望这能帮到你!

1
我找到了一个解决方法,虽然不推荐使用,但如果你真的想要完整地转储对象,可以将以下代码片段添加到 /bootstrap/autoload.php 中。
if (! function_exists('dd')) {
    /**
     * Dump the passed variables and end the script.
     *
     * @param  mixed
     * @return void
     */
    function dd()
    {
        array_map(function ($x) {
            $dumper = 'cli' === PHP_SAPI ? new \Symfony\Component\VarDumper\Dumper\CliDumper() : new \Illuminate\Support\Debug\HtmlDumper();
            $cloner = new \Symfony\Component\VarDumper\Cloner\VarCloner();
            $cloner->setMaxItems(-1);
            $cloner->setMaxString(-1);
            $dumper->dump($cloner->cloneVar($x));
       }, func_get_args());

        die(1);
    }
}

这段代码必须添加在以下代码之前:

require DIR.'/../vendor/autoload.php';

它会覆盖 Laravel 的 dd 函数,并在转储之前设置 Symfony VarCloner 对象的 'setMaxItems' 和 'setMaxString'。


1

dd()并没有错误,只是基础的Sympony VarDumper配置问题。

我认为有问题的那一行是这个, 它将$maxDepth设置为20,但我还没有检查过。

查看Laravel Dumper逻辑, 似乎没有办法从Laravel内部覆盖此设置。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接