Chapter 1. Introducing Laravel 29 The debugging and development utilities discussed in this section are specific to Laravel, and do not take into account the many other tools available to PHP in general. Be sure to check out Xdebug⁴⁵, FirePHP⁴⁶, and the many tools integrated into PHP IDEs such as Zend Studio⁴⁷ and PHPStorm⁴⁸. The dd() Function Ensuring the .env file’s APP_DEBUG variable is set to true is the easiest way to view information about any application errors, because Laravel will dump error- and exception-related information directly to the browser. However, sometimes you’ll want to peer into the contents of an object or array even if the data structure isn’t causing any particular problem or error. You can do this using Laravel’s dd()⁴⁹ helper function, which will dump a variable’s contents to the browser and halt further script execution. For example suppose you defined an array inside a Laravel application and wanted to output its contents to the browser. Here’s an example array: 1 $items = [ 2 'items' => [ 3 'Pack luggage', 4 'Go to airport', 5 'Arrive in San Juan' 6 ] 7 ]; You could execute the dd() function like so: 1 dd($items); Passing $items into dd() will cause the array contents to be dumped to the browser window as depicted in the below screenshot. ⁴⁵http://xdebug.org/ ⁴⁶http://www.firephp.org/ ⁴⁷http://www.zend.com/en/products/studio ⁴⁸http://www.jetbrains.com/phpstorm/ ⁴⁹https://laravel.com/docs/master/helpers#method-dd
Chapter 1. Introducing Laravel 30 dd() function output Of course, it is likely at this point you don’t know where this code would even be executed. Not to worry! In the chapters to come just keep this and the following solutions in mind so you can easily debug your code once we start building the application. The Laravel Logger While the dd() helper function is useful for quick evaluation of a variable’s contents, taking advantage of Laravel’s logging facilities is a more effective approach if you plan on repeatedly monitoring one or several data structures or events without interrupting script execution. Laravel will by default log error-related messages to the application log, located at storage/logs/laravel.log.