How To Use Dumpable Trait In Laravel 11

  Mar 2024
  ITSolutionsGuides
  Category: Laravel
How To Use Dumpable Trait In Laravel 11

Welcome To ITSolutionsGuides,


Laravel 11 provides the new dumpable trait for the debugging process. We should add the dumpable trait in our model in order to make the model data dumpable , after adding the dumpable trait we need to use dump() or dd() method in our controller file in order to dispplay the data in the output screen.
To utilize the Dumpable trait in Laravel 11, start by importing it into your model. Next, define anyproperty array containing attributes to include in dumps. Then, simply use the dump() or dd() method on your model instance to output specified attributes. This trait aids in debugging and controlling data output with ease.

Let's Start Coding

Lets create the model and add the
use Illuminate\Support\Traits\Dumpable; on the top of the model and then we need to use the Dumpable traits by declaring
use Dumpable;


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'
    ];
}

Let's Create Controller

In the controller we will create the function to fetch the product details and then we will use the Dumpable trait by using dd() , In the output screen we will see the data.


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();
    }
}

We hope it helps everyone. Thanks for supporting ITSolutionsGuides and keep supporting us also follow us in social media platforms.

Subscribe for NewsLetter

Be the first to know about releases and tutorial news and solutions.

We care about your data in our privacy policy.

ITSolutionsGuides

ITSolutionsGuides was started mainly to provide good and quality web solutions for all the developers. We provide tutorials to support all the developers and also we try to provide solutions to the errors we face while coding.

Contact US

ITSolutionsGuides, provide users with an easy-to-use form to reach out for support or inquiries.

whatsapp  gmail  instagram-new--v1  facebook-circled  twitter-circled  linkedin  github  pinterest 

Copyright © 2023 - 2024 All rights reserved | ITSolutionsGuides