Using dd command to debug in Laravel

Posted on

This is an article which is describing on how to use dd command in order to easily help programmer or developer in order to do a debugging process. The command is actually can only be done in a script for web application based on Laravel PHP framework. The command ‘dd’ is actually stands for ‘Dump and Die’,   and it is basically showing the content of a variables in the browser for a simple debugging process to find out whether errors are available by analyzing the content of the variables printed in the browser.

The command or the short helper function which is specifically used to show variable is quite convenient to be utilized in the script based on  Laravel PHP framework. Just type the following command in the script for an example :

dd($selected_server);

In the above command included in the script, ‘$selected_server’ is a variable which is going to be inspected further using the command ‘dd’. Below is the example of the display in an image :

Using dd command to debug in Laravel
Using dd command to debug in Laravel

The content can be shown in the following output below :

Collection {#210 ▼
  #items: array:1 [▼
    0 => {#211 ▼
      +"id_server": 82
      +"type": "XX Server XXX"
      +"sn": "-"
      +"hostname": "XXXXXXXXX"
      +"label": "-"
      +"ip_internal": "xxx.xxx.xxx.xxx"
      +"ip_public": "-"
      +"ip_management": "-"
      +"processor": "vCPU 2 Core"
      +"memory": "X GB"
      +"disk": "XXX GB"
      +"os": "Linux CentOS 7"
      +"software": "PostgreSQL, MySQL"
      +"status": "Development"
      +"spec": "Application"
      +"condition": "Good"
      +"year": "-"
      +"warranty": "-"
      +"location": "DC Pusintek"
      +"cp": "Agus Sunarto, Hamdi"
      +"info": "Server Development Aplikasi New SIKD"
      +"id_user": 15
      +"id_app": 0
      +"id_db": 0
      +"name": "Mike Powell"
      +"position": null
    }
  ]
}

As it can be seen, the output is just displaying the content of the necessary variable picked or selected to be displayed further. So, developer or programmer will not be distracted with other content printed by using another command for dumping the content of a variable such as ‘print_r’ or maybe ‘var_dump’. In this context, the content of the variable is displayed until the lowest content or the deepest level that can be reached in the form of pair element. The pair element is represented in a form of “key” : value.

To reset the display into the original one, just erase the ‘dd’ line of code part or simply comment it.

One thought on “Using dd command to debug in Laravel

Leave a Reply