How to use new Dumpable Trait in Laravel 11?

By Hardik Savani September 4, 2024 Category : Laravel

Hi,

In this post, i will show you how to use new Dumpable Trait in laravel 11 framework.

The release of Laravel 11 is around the corner and it is packed with a lot of new features and improvements. Laravel 11 comes with a slimmer application skeleton. Laravel 11 introduce streamlined application structure, per-second rate limiting, health routing etc.

Laravel 11 introduces a dumpable trait, streamlining debugging processes. In this example, we will create Product model and use the Dumpable Trait on it. Then, you can easily debug it using dump() and dd() methods.

You can see the simple example code:

Let's create a Product.php model and use the Dumpable trait as shown below:

app/Models/Product.php

<?php
   
namespace App\Models;
  
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Traits\Dumpable;
  
class Product extends Model
{
    use Dumpable;
  
    /**
     * Write code on Method
     *
     * @return response()
     */
    protected $fillable = [
        'name', 'body', 'status'
    ];
}

Now, you can simply debug it in your controller like the code below:

app/Http/Controllers/ProductController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\Product;
  
class ProductController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $products = Product::latest()->get();
  
        $products->dd();
    }
}

You can see the below output:

Laravel 11 Dumpable Trail 2

I hope it can help you...

Shares