Skip to content

Instantly share code, notes, and snippets.

@deepak-cotocus
Last active August 22, 2020 10:27
Show Gist options
  • Save deepak-cotocus/d64f03c1e63109e0a5d95bdb5a630ca5 to your computer and use it in GitHub Desktop.
Save deepak-cotocus/d64f03c1e63109e0a5d95bdb5a630ca5 to your computer and use it in GitHub Desktop.
How to Create Laravel Eloquent API Resources to convert model collections into JSON(Part 4).

Json resource without wrapping with data key

Introduction:

In the earliear examples i was genreting Json resource which is wrapped with "data:"{} Object like:

Let's generate json resource without data wrapping..

{
"data": {
"id": 3,
"name": "Deepak",
"email": "deepakkumar.ac.in@gmail.com",
"profile": "http://demo-app/user/3"
},
"version": "2.0.0",
"attribution": "http://demo-app/terms-of-service",
"valid_as_of": "Sat, 22 Aug 2020 08:25:40"
}

Now, Here we will see how to generate Json resource without any wrapping.

  • Lets see where i will make Changes in code. NOte: Make sure you have Resourec folder with User class under <Your-app>\app\http\Resources

STEP 1: Adding with(){} with additional information.

<?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 [
            'id' => $this->id,
            'name' => $this->name,
            'email' => $this->email,
            'profile' => url('/user/'. $this->id . '/'),
     
        ];
    }
}
 

STEP 2: Go to Providers and open <your-app>/app/Providers/AppServiceProviders and add below code:

We will tell laravel to not to render Json resource with Wrapping

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Http\Resources\Json\Resource;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Resource::withoutWrapping();   
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

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;


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

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

	$users = User::first();
    return new UserResource($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

json-without-wrapping-with-data-key

My basic recommendation for learning : Eloquent: API Resources

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment