Laravel 的 DD 辅助函数是不是正常工作?

Posted

技术标签:

【中文标题】Laravel 的 DD 辅助函数是不是正常工作?【英文标题】:Is Laravels' DD helper function working properly?Laravel 的 DD 辅助函数是否正常工作? 【发布时间】:2016-03-27 20:11:33 【问题描述】:

我喜欢使用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"
        

还有其他人经历过这种情况吗?

【问题讨论】:

【参考方案1】:

这是一个不为人知的警告,即返回过大的结果列表。通常,dd() 旨在快速概览您返回的数据,并在较小的数据组上很好地处理“向下钻取”。一旦达到某个数字(我忘记了确切的数字,也许是 500?),该功能就不再起作用了。

如果您绝对需要在代码中某处使用它之前查看此数据,请在get() 结果之前使用limit() 子句,或使用dd($example[0]) 查看单个结果的详细信息。希望对您有所帮助!

【讨论】:

【参考方案2】:

dd() 中的错误不是基础Sympony VarDumper 的配置方式。

我相信有问题的行是this one,它将$maxDepth 设置为20,但我没有检查过这个。

查看 Laravel Dumper logic 似乎无论如何都无法从 Laravel 中覆盖它。

【讨论】:

【参考方案3】:

我找到了解决这个问题的方法,虽然不推荐,但如果你真的想要转储整个对象,请将以下代码 sn-p 添加到 /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);
    

这必须添加到行上方:

需要DIR.'/../vendor/autoload.php';

它覆盖 laravel dd 函数并在转储之前在 Symfony VarCloner 对象上设置 'setMaxItems' 和 'setMaxString'。

【讨论】:

以上是关于Laravel 的 DD 辅助函数是不是正常工作?的主要内容,如果未能解决你的问题,请参考以下文章

PHP 全局使用 Laravel 辅助函数 dd

laravel 辅助函数

Laravel/Php - 在急切加载关闭中修改集合?

自定义laravel函数dd()

Laravel 常用辅助函数

laravel 5.5 《电商实战 》辅助函数