Laravel Debugging Methods With Example

  • 28-05-2022
  • 1547
  • Laravel 9
  • Haresh Chauhan

When you are developing a big application, So it is critical to make sure all that exchanging the data from the client-side to the database server is correct or not. Essentially, Definitely yes it needs to debug how the data is correct and it's going on the right path.

Also sometime developer need to debug because of finding BUGS and ERRORS. So the developer will check different ways of coding are the right coding or not.

So in this post, you will learn the different debugging methods in laravel. like dd(),dump() etc..., Sometime developer just quick checking what expected result that's all need fix issues and errors that's all.

There are so perfect and essential necessary tools available for laravel. Laravel Debugger and Laravel Telescope. But good that is laravel has some already built functions and some tools for this debug.

So let's look at it.

dd()

dd stand for the "Dump and Die". every laravel developer must know before starting learning laravel. This method will print a variable and outing the variable result and stoping further executing the code while the dd method is there.

$var = 'Webappfix.com';

dd($var);

This is the output:

"Webappfix.com"

Further full example from laravel collection from database query table.

<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {

    $user = App\User::get();

    dd($user);

    return view('post');
});

This will be your output in your browser.

Illuminate\Database\Eloquent\Collection {#1053 ▼
#items: array:1 [▼
    0 => App\User {#986 ▼
    #fillable: array:4 [▶]
    #hidden: array:2 [▶]
    #casts: array:1 [▶]
    #connection: "mysql"
    #table: "users"
    #primaryKey: "id"
    #keyType: "int"
    +incrementing: true
    #with: []
    #withCount: []
    #perPage: 15
    +exists: true
    +wasRecentlyCreated: false
    #attributes: array:9 [▼
        "id" => 1
        "name" => "Joy"
        "email" => "joy123@gmail.com"
        "email_verified_at" => "2022-05-28 22:39:31"
        "password" => "123456"
        "post" => "post data"
        "remember_token" => "dsfsdfsfsdfsdf"
        "created_at" => null
        "updated_at" => null
    ]
    #original: array:9 [▶]
    #changes: []
    #classCastCache: []
    #dates: []
    #dateFormat: null
    #appends: []
    #dispatchesEvents: []
    #observables: []
    #relations: []
    #touches: []
    +timestamps: true
    #visible: []
    #guarded: array:1 [▶]
    #rememberTokenName: "remember_token"
    }
]

You can see the above example output from the user table whole return in the browser tree structure.

You'll note that method stoping executing the next code that the blade file in return view('post') is not able to return on this blade file.

In the blade file you can debug using this method like:

{{ dd($user) }}

Another way:

@php dd($user) @endphp

dump()

The dump is the alternative debugging method instead of dd. dump method will output the same it will not stoping further executing code. You can use it the same place where you used dd.

We will take an earlier example as dump().

$var = 'Webappfix.com';
    
dump($var);

This is the output:

"Webappfix.com"

Same again for Further full example from laravel collection from database query table.

<?php
    
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {

    $user = App\User::get();

    dump($user);

    return view('post');
});

Output result in browser:

post.blade.php | return view('post')

image

Same output result but it will not stop further executing the code. It will return the blade file there also this debug will show.

dump mainly used for looping inside and setting the collection of the array. That time this debug method will use.

So these two main laravel debugging tool using we can correct the data and fix the bug through this methods.

Thanks...


We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :