how to Transform collections in laravel

Quite often, Eloquent automatically takes a collection to transform it into something that you can output in a better way. For example, here is the code that I am using to show a list of a magazine website’s news categories:

  $categories = \App\Category::all();
   return $categories;

This is the corresponding output:

  [
    {
      id: 1,
      name: "Editorial",
      slug: "editorial",
      description: "Qui est quo asperiores aliquid vitae possimus. Dolor consequuntur similique voluptatem a laborum dolorem ea repellendus. Aspernatur ducimus quis dolorum consequatur vel nam at. Aut omnis rem laborum.",
      created_at: "2015-04-21 10:14:37",
      updated_at: "2015-04-21 10:14:37"
    },
    {
      id: 2,
      name: "Interview",
      slug: "interview",
      description: "Rerum deleniti rerum aliquid laudantium id non voluptatum. Aut quia distinctio consequatur velit natus inventore sunt iusto. Non totam quis quam sint et.",
      created_at: "2015-04-21 10:14:37",
      updated_at: "2015-04-21 10:14:37"
    },
    {
      id: 3,
      name: "Reportage",
      slug: "reportage",
      description: "Adipisci et veritatis excepturi ullam explicabo. Eos dolore quas a vero. Optio voluptatem accusamus ex optio. Rerum rem quaerat qui maiores.",
      created_at: "2015-04-21 10:14:37",
      updated_at: "2015-04-21 10:14:37"
    }
  ]

However, now consider the following code:

  $categories = \App\Category::all();
   return $categories;

Then, let’s change the code to this:

$categories = \App\Category::all();
dd($categories);

This is what happens:

  Collection {#152
    #items: array:3 [
      0 => Category {#155
        #connection: null
        #table: null
        #primaryKey: "id"
        #perPage: 15
        +incrementing: true
        +timestamps: true
        #attributes: array:6 [
          "id" => 1
          "name" => "News"
          "slug" => "news"
          "description" => "Qui est quo asperiores aliquid vitae possimus. Dolor consequuntur similique voluptatem a laborum dolorem ea repellendus. Aspernatur ducimus quis dolorum consequatur vel nam at. Aut omnis rem laborum."
          "created_at" => "2015-04-21 10:14:37"
          "updated_at" => "2015-04-21 10:14:37"
        ]
        #original: array:6 [
          "id" => 1
          "name" => "News"
          "slug" => "news"
          "description" => "Qui est quo asperiores aliquid vitae possimus. Dolor consequuntur similique voluptatem a laborum dolorem ea repellendus. Aspernatur ducimus quis dolorum consequatur vel nam at. Aut omnis rem laborum."
          "created_at" => "2015-04-21 10:14:37"
          "updated_at" => "2015-04-21 10:14:37"
        ]
        #relations: []
        #hidden: []
        #visible: []
        #appends: []
        #fillable: []
        #guarded: array:1 [
          0 => "*"
        ]
        #dates: []
        #casts: []
        #touches: []
        #observables: []
        #with: []
        #morphClass: null
        +exists: true
      }
      1 => Category {#156 ...}
      2 => Category {#157 ...}
    ]
  }

Wait, wait; what? Just changing a dd() call with a return? Well, you can see this magic using two special methods: toArray and toJSON. You can also use them manually, if you need, just like this:

  $books = \App\Book::all();

  $toArray = $books->toArray();
  $toJson = $books->toJson();

Cool, right?

 

deploy your laravel app in cloud now for free and get 10$ credit

About the author

Deven Rathore

I'm Deven Rathore, a multidisciplinary & self-taught designer with 3 years of experience. I'm passionate about technology, music, coffee, traveling and everything visually stimulating. Constantly learning and experiencing new things.

Pin It on Pinterest

Shares