📜 ⬆️ ⬇️

First impressions of the Laravel API Resources

image

Last night, Taylor Otwell finally presented what could be the beginning of replacing Fractal when developing an API on a Laravel 5.5 framework. This is my trial version of the article, let's go.

Interesting material begins with the 4th step.

1. Install the application


composer create-project laravel/laravel responses dev-develop cd responses touch database/database.sqlite php artisan make:model Post -mfa php artisan make:resource UsersWithPostsResource php artisan make:resource PostsResource php artisan make:controller UsersController --resource 

Write in the project .env file the use of the SQLite database, removing all the others.

 DB_CONNECTION=sqlite 

2. Database preparation


2.1. Let's create a The posts migration
')
 Schema::create('posts', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->string('body'); $table->unsignedInteger('user_id'); $table->timestamps(); }); 

2.2. Create a factory: database/factories/PostFactory.php

 <?php use Faker\Generator as Faker; $factory->define(App\Post::class, function (Faker $faker) { return [ 'title' => $faker->sentence, 'body' => $faker->paragraph, 'user_id' => function () { return factory(\App\User::class); } ]; }); 

2.3 Add a relation to the app/User.php

 public function posts() { return $this->hasMany(Post::class); } 

2.4 Avoid mass assignment in app/Post.php

 <?php namespace App; use Illuminate\Database\Eloquent\Model; class Post extends Model { protected $guarded = []; } 

2.5 Filling the database with test data

 php artisan migrate:fresh php artisan tinker factory(App\Post::class)->times(2)->create(); factory(App\Post::class)->times(2)->create(['user_id' => 1]); 

3. Configuring Routes


 Route::apiResource('/users', 'UsersController'); 

4. Conversion of the model into resources


 /** * Display a listing of the resource. * * @param User $user * @return \Illuminate\Http\Response */ public function index(User $user) { return new UsersWithPostsResource($user->paginate()); } 

5. Users with resource posts


 <?php namespace App\Http\Resources; use Illuminate\Http\Resources\Json\Resource; class UsersWithPostsResource extends Resource { /** * Transform the resource into an array. * * @param \Illuminate\Http\Request * @return array */ public function toArray($request) { // Eager load $this->resource->load('posts'); return $this->resource->map(function ($item) { return [ 'name' => $item->name, 'email' => $item->email, 'posts' => new PostsResource($item->posts) ]; }); } } 

6. Post resources


 <?php namespace App\Http\Resources; use Illuminate\Http\Resources\Json\Resource; class PostsResource extends Resource { /** * Transform the resource into an array. * * @param \Illuminate\Http\Request * @return array */ public function toArray($request) { return $this->resource->map(function ($item) { return [ 'title' => $item->title ]; }); } } 

7. Conclusion


The first obvious difference in comparison with Fractal is that the resource has simple and direct access to the entire collection, and not to every object. This means that when converting a collection of users, you can easily load each record without N + 1 requests.

You can easily transform the “dots”, as you can simply create a new resource class to transform your data as needed.

I want to expand on this topic in more detail as soon as I get more features from the Laravel API Resources.

From translator


I considered this topic interesting and decided to translate with language adaptation. Please do not kick your feet. Links to the original and translated docking on this topic are also welcome.

Source: https://habr.com/ru/post/336432/


All Articles