ホーム > Laravel > How to Debug in Laravel
Laravel

How to Debug in Laravel

Thank you for your continued support.
This article contains advertisements that help fund our operations.

⇨ Click here for the table of contents for Laravel articles

This article introduces some tips on how to debug in Laravel.

What is Debugging?

In this article,

It refers to fixing program bugs so that it works correctly.

Debugging When Using Blade (excluding API)

Basically, it is good to debug using the dd() function.

What does dd do?

It allows you to stop the process and check the contents of variables.

By stopping the process with dd, the location of errors becomes more detailed

By stopping the process with dd and outputting variables,

You can make the "location of the error more detailed".

Instead of blindly fixing the code inside the function, knowing specifically which line is causing the error makes a big difference.

Even if you use dd() after the code causing the error, the error will still be displayed,

You can recognize that "the cause is further back".

Using dd in a Controller

  $users = User::all();
  dd($users);

  // Result
  // Illuminate\Database\Eloquent\Collection {#1039 ▼
    #items: array:6 [▶]
// }

Using dd in views (blade)

If you use variables in a blade file, errors will occur.

You can use dd at that time as well.

// blade file
{{ dd($users) }}

In the case of blade files, the code before the process is stopped is read, so the display will appear to be loaded halfway.

Debugging Methods for APIs

How to check the contents of errors

By default,

storage/logs/laravel.log

Stores the contents of errors. The latest one should be at the bottom.

Debugging variables

The easiest way is to return the response.

Since the processing after return does not proceed, you can more detailed the position of the bug similar to dd.

$users = User::all();
return response()->json(compact('users'),200);
// The following does not proceed
$posts = Post::matigatterukoudo();

With the above code, you can see what the current users variable contains.

Debugging variables ②

A method to check the contents of variables without stopping the process.

\Log::info($users);

By writing like this, you can output to the log file.

By checking storage/logs/laravel.log, the contents of the variables are output.

Summary

That's all.

I wrote my own debugging methods.

When working with APIs, I recommend using tools like Postman for quick verification.

If you have any opinions or feedback, please DM me on Twitter!

That's it!

Popular articles

Deploying a PHP7.4 + Laravel6 Project to AWS EC2

Implementing Breadcrumbs in Laravel with laravel-breadcrumbs

Please Provide Feedback
We would appreciate your feedback on this article. Feel free to leave a comment on any relevant YouTube video or reach out through the contact form. Thank you!