Where to use the dd() function to debug Laravel application?
Asked Answered
T

4

9

I need to find know how to properly use the dd() function in Laravel projects.

For example - I have got tasks to debug some code and functionality in my project (PHP /Laravel) and it always takes me forever to find the exact file or folder or code where the problem is.

My mentor says to use dd() functiom to find things faster (but for learning purposes he didn't explain me a lot about how to actually use it and said to find out myself), but said that I should start with Route (we use backpack as well for our project). So after finding Route (custom.php file) which controller connects to my required route what should I do next?

How do I implement the dd() function (or as my mentor says dd('call here') function) to quickly find what I should be looking for to solve my problem and complete my task?

Where should I write this dd() and how should I write it?

For example, I have:

public function create(): View
{
    return view('xxxxxx. \[
        //
        //
    \]);
}

and if I put dd() anywhere in the code, I get error message in my URL.

Transmitter answered 26/3, 2022 at 14:33 Comment(0)
H
7

first of all ,in Laravel we use dd() before return in order to read any variable. in controller we often use two kinds of variables : collection(which we get its members via foreach) or singular variable (we get it via its name)for example:$var = 1; dd($var). notice: if you are using ajax response you will not be able to see dd() results in page ,you can see the result via network tab in your browser (if u inspect your page).

Hurdle answered 26/3, 2022 at 15:23 Comment(0)
S
11

dd stands for "Dump and Die."

Laravel's dd() function can be defined as a helper function, which is used to dump a variable's contents to the browser and prevent the further script execution.

Example:

dd($users,$variable1,$var2);

You can use dd() in blade

@foreach($users as $user)
  @dd($user)
  OR
  {{dd($user)}}
@endforeach

@dd($var1)

You can read this article, the have more example and comparison https://shouts.dev/articles/laravel-dd-vs-dump-vs-vardump-vs-printr-with-example

Strongroom answered 26/3, 2022 at 14:44 Comment(2)
dd() called inside of a loop makes NO sense, right? https://mcmap.net/q/1323910/-script-execution-foreach-loop-stops-when-i-call-laravel-39-s-dd-function-duplicate/2943403Perugia
See Flame's comment: #59734667Perugia
H
7

first of all ,in Laravel we use dd() before return in order to read any variable. in controller we often use two kinds of variables : collection(which we get its members via foreach) or singular variable (we get it via its name)for example:$var = 1; dd($var). notice: if you are using ajax response you will not be able to see dd() results in page ,you can see the result via network tab in your browser (if u inspect your page).

Hurdle answered 26/3, 2022 at 15:23 Comment(0)
S
0

As Laravel is following model-view-controller or MVC design pattern. First go to the route and check which controller is called in the URL with the related URL. Then go to the controller. **dd**() function is basically a dump and die. you also can do this by **print** or **echo** function too.

Lets assume that I have a controller name ProductController where I have method name index.From where I need to show a list of products in a table.

// in controller

public function index()
{
  $products = Products::all();

  // here you think ,I need to check whether I am getting the output or 
  not.

  dd( $products );
//Or  echo $products;

  return view ('product.list',compact('products'));
}

let's suppose you are getting everything but in view when you loop through the products you declare the wrong variable name or mistakenly do some spelling mistakes. and want to see the result.

In view just do the dd() method by the following way:

{{ dd($products) }}
Stepdame answered 26/3, 2022 at 18:44 Comment(1)
See Flame's comment: #59734667Perugia
C
0

dd() kills the script at that line and prints its contents to the screen. This can be a quick way to see a value, but we more often want to see a script continue and log the content. For that we use logging https://laravel.com/docs/10.x/logging#writing-log-messages :

Log::debug($message);

Then tail your Laravel log:

tail -f storage/logs/laravel.log

You'll see other useful information in those logs to highlight what's going wrong. I like adding some weird characters so I can grep to only show my messages. Like

Log::debug('*** ' . $message);

tail -f storage/logs/laravel.log | grep "***"

Condense answered 12/3 at 0:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.