3

how can I increase the laravel 8 dd() limitations? I have lots of nested relationship and I cant view most of them due to many data. For example this

  #relations: array:4 [▼
    "activecourses" => Illuminate\Database\Eloquent\Collection {#1346 ▼
      #items: array:3 [▼
        0 => App\Models\Studentcourse {#1353 ▶}
        1 => App\Models\Studentcourse {#1354 ▼
          #guarded: []
          #connection: "mysql"
          #table: "studentcourses"
          #primaryKey: "id"
          #keyType: "int"
          +incrementing: true
          #with: []
          #withCount: []
          +preventsLazyLoading: false
          #perPage: 15
          +exists: true
          +wasRecentlyCreated: false
          #escapeWhenCastingToString: false
          #attributes: array:22 [ …22]
          #original: array:22 [ …22]
          #changes: []
          #casts: []
          #classCastCache: []
          #attributeCastCache: []
          #dates: []
          #dateFormat: null
          #appends: []
          #dispatchesEvents: []
          #observables: []
          #relations: array:1 [ …1]
          #touches: []
          +timestamps: true
          #hidden: []
          #visible: []
          #fillable: []
        }
        2 => App\Models\Studentcourse {#1355 ▶}
      ]
      #escapeWhenCastingToString: false
    }

I found this link Laravel dd function limitations but I think its outdated since the new dd function for laravel 8 is

this is from vendor folder.

use Symfony\Component\VarDumper\VarDumper;

if (!function_exists('dump')) {
    /**
     * @author Nicolas Grekas <p@tchwork.com>
     */
    function dump($var, ...$moreVars)
    {
        VarDumper::dump($var);

        foreach ($moreVars as $v) {
            VarDumper::dump($v);
        }

        if (1 < func_num_args()) {
            return func_get_args();
        }

        return $var;
    }
}

if (!function_exists('dd')) {
    /**
     * @return never
     */
    function dd(...$vars)
    {
        if (!in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) && !headers_sent()) {
            header('HTTP/1.1 500 Internal Server Error');
        }

        foreach ($vars as $v) {
            VarDumper::dump($v);
        }

        exit(1);
    }
}

Tried adding this lines before dd() but doesnt work

   ini_set('xdebug.var_display_max_depth', 100);
   ini_set('xdebug.var_display_max_children', 2000);
   ini_set('xdebug.var_display_max_data', 2000);

Is there any way or alternative in viewing nested data? I dont want to use var_dump or print_r since the browser becomes slow when there are lots of data. Thanks in advance.

gp_sflover
  • 3,460
  • 5
  • 38
  • 48
draw134
  • 1,053
  • 4
  • 35
  • 84
  • 3
    i usually convert the data to array before "dding" it, so it only shows the data of the array and other unneeded object stuff is not shown. maybe is an option for you – ericmp Nov 09 '22 at 08:14
  • 2
    Laravel has Monolog built in, so use Log::info($data); and it will write the result to a file in storage/logs within your project folder. – VDTe Nov 09 '22 at 08:15
  • @ericmp it has only the same outptut even in array form. – draw134 Nov 09 '22 at 08:17
  • I think that is limitation of `var_dump` either you can convert it to json and try or better option is using `Log` to get it in the log file. – Prafulla Kumar Sahu Nov 12 '22 at 17:58
  • 3
    Hello, possible duplicate of https://stackoverflow.com/questions/39011241/laravel-dd-function-limitations – Mtxz Nov 13 '22 at 03:18
  • @Mtxz 1. OP wants a solution which do not take much loading time as it the data is large. 2. That question is for older version of laravel – Leena Patel Nov 13 '22 at 11:12
  • As @VincentDaveNavaresTe suggested try printing in log file – Leena Patel Nov 13 '22 at 11:14
  • 1
    @LeenaPatel thank you for your feedback. The proposed solutions on the linked post are still valid for Laravel 8.x, I just tested it (see my updated answer). The limitation is the browser that will crash because of the huge DOM size. – Mtxz Nov 13 '22 at 18:13
  • @LeenaPatel indeed the solution is not complete, and should only be used for this particular debug purpose. – Mtxz Nov 13 '22 at 18:36
  • try use laravel dump server without any limit https://beyondco.de/docs/laravel-dump-server/installation – Ramil Huseynov Nov 16 '22 at 21:22

4 Answers4

2

This question has already been answered here: Laravel dd function limitations - EDIT: those still work but are not complete anymore (VarDumper overrides), and should not be used in production.

Override Laravel dump

The Dumper is using Symfony's VarCloner which is extending the AbstractCloner. This class has a $maxItems attribute set to 2500.

Basically, you need to override Laravel Dumper as explained in the post's answers. You can also create a new method like ddd() allowing larger outputs with a custom Dumper.

Note that a very large dataset can crash your browser tab/window as the DOM size will grow with it.

Alternatives

Alternatively, if you really need to view this data "live" (ex: an API route), you could return JSON and check them with Postman or Chrome debugger (network tab, parsed payload). They can parse and provide a "dynamic" data-view like Laravel dump does with HTML/CSS/JS.

Very large JSON (+50/100mo)

For very large JSON, I would recommend:

  • Downloading it as a JSON file
  • View it with a large JSON viewer/parser (I use the paid UltraEdit for 500mo+ JSON. Some freeware are enough for smaller files. Postman/Chrome are OK for 50mo I guess). Same, they can parse and provide a dynamic view like dd/dump.

EDIT:

I just tested "Lucas Martin" solution on Laravel 8.6.12 and it works as expected:

EDIT 2: works but is not complete, the dumper is more complex in 8.X than 5.x (server format). Logic is the same but provided the override is not complete anymore. I would only use it for this particular debug case. I would use this simple/old logic with a custom method. Default (truncated):

enter image description here

VarDumper updated handler (full):

enter image description here

The limitation of this solution will be the browser. It'll crash with a huge dataset as DOM size becomes too much to store in memory. Also, I would only use it in development environments.

Laravel debugbar debug helper may be able to handle larger objects and dynamic view, but still limited to browser memory and DOM size.

credit: random JSON generator

Mtxz
  • 3,749
  • 15
  • 29
1

There are three ways I use the dd() method.

  1. Printing to command line (either via written test or artisan tinker)
  2. Printing to browser. You can do this if you're making use of Blade.
  3. Printing to a Log file. Use the Log facade, for example Log::debug($activeForces)

Due to the large amount of data, it won't even be advisable to print out the content of your variable to screen. Try using the log file.

Alternatively, why don't you narrow down the content being die-dumped? For example: dd($activeForces[0])

Eskay Amadeus
  • 275
  • 2
  • 11
1

So based on what you want, as well as what I've used. Tracy might be a better tool than dd() Visit their GitHub Here

This replaces the default debug system in Laravel with the Tracy PHP debugger created by the team at Nette.

This means instead of using dd($myArray) you would use bdump($myArray,"Active Courses") (The second Parameter is optional but useful when using multiple bdumps() on a single page so you know which is which)

This will also allow you to see the rendered page as well as the dump (And other useful features, such as the Route, Session and even the Laravel Terminal)

CloudTheWolf
  • 334
  • 2
  • 10
1

In this case the many data is not due to what is under the model. Just dd() each of the models you want to see.

dd($variable['activecourses'][0]);

Either way your reason for not using dump (Browser getting slow) makes no sense. cause DD or anything else with interactive javascript and styling of the same data is always gonna be way slower. Increasing your execution time and memory limit for php is not gonna change that.

Another option is to dump the data to a file and open it with something efficient like Sublime.

Neo
  • 11,078
  • 2
  • 68
  • 79