🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

How to Create Laravel Eloquent API Resources to convert model collections into JSON(Part 5).

Laravel Migration

Resource Collection

Introduction:

In addition to generating resources that transform individual models, you may generate resources that are responsible for transforming collections of models. This allows your response to include links and other meta information that is relevant to an entire collection of a given resource.

Collection resources extend the Illuminate\Http\Resources\Json\ResourceCollection class:

In previoes lessons we have seen how to generate json data of single object. Now we are going to see how to generatet Collection of Objects.

STEP 1: Command to generate collectionResource class.

php artisan make:resource UserCollection

Or

php artisan make:resource User --Collection

collecton-resource

  • Now we anve another resource UserCollection

collection-resource1

STEP 2:

Keep <your-app>\app\Http\Resources\User as it is.

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\Resource;
use Illuminate\Support\Facades\Log;

class User extends Resource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            
            'name' => $this->name,
            'email' => $this->email,
            'profile' => url('/user/'. $this->id . '/'),
     
        ];
    }
}
 

STEP 3:

Open <your-app>\app\Http\Resources\UserCollection.

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;

class UserCollection extends ResourceCollection
{
    /**
     * Transform the resource collection into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return parent::toArray($request);
    }
}

 

STEP 2: keep below below Changes in web.php file as it was before

<?php

use App\User;
use App\Http\Resources\User as UserResource;
use App\Http\Resources\UserCollection;


Route::get('/', function () {
    return view('welcome');
});

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

	$users = User::all();
    return new UserCollection($users);
});

STEP 3: All set to go

  • Here my Application is 'demo-app' and its virtual host url is http://demo-app/. So i will open browser and hit: http://demo-app/json and see tha magic of LARAVEL RESOURCES

collection-resource2

My basic recommendation for learning : Eloquent: API Resources

Thanks

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.