support@codebucket.net

How to build API resources using Laravel

How to build API resources using Laravel

K. M. Shawkat Zamil | January 03, 2023

Hello Developers,

 

Do you know what is API resource in Laravel? If you don’t, I am here for you. API resource is a layer situated between our Eloquent models and the JSON responses that are actually returned by API.

 

You can always Eloquent models or collections to JSON using the toJson method. However, Eloquent resources provide more granular and robust control over the JSON serialization of your models and the relationships of the model.  See this tutorial for a better understanding.

 

Let’s create it from the scratch, you can make all the elements like controllers, models, migration, and seeder. Request, policy by the following command:

 

php artisan make:model Author -a -r

 

 

 

Let’s complete the migration file by adding the fields we needed in the database table. Open the author migration that we create.

 

[project_directory]/database/migrations/[date]_create_authors_table.php

<?php
 
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
 
class CreateAuthorsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('authors', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->timestamps();
        });
    }
 
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('authors');
    }
}

 

Then we need to open the Author model. Write below the use HasFactory, $protected array, Adding the protected fillable make sure that we can add the name when we use the eloquent create method.

 

[project_directory]/app/Models/Author.php

<?php
 
namespace App\Models;
 
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
 
class Author extends Model
{
    use HasFactory;
 
    protected $fillable = ['name'];
}

 

 

I think we are ready to migrate the database. Run the below command:

 

php artisan migrate

 

We have created the table. Now we need data. To insert data we can use the factory that we created earlier. In the AuthorFactory.php file, we can find the definition() method that can be used to generate data. As our table has only one column to write, paste the below code to the factory file.

 

[project_direcrtory]/database/factories/AuthorFactory.php

<?php
 
namespace Database\Factories;
 
use Illuminate\Database\Eloquent\Factories\Factory;
 
class AuthorFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'name' => $this->faker->name
        ];
    }
}

 

 

In order to call our factory, we need to have a database seeder. Open the seeder folder, we need to start with the DatabaseSeeder.php

 

[project_directory]/database/seeders/DatabaseSeeder.php

<?php
 
namespace Database\Seeders;
 
use Illuminate\Database\Seeder;
 
class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        $this->call(AuthorSeeder::class);
    }
}

 

 

Now open the AuthorSeeder.php. We need to add some code in the run() method. We can write the number inside the factory() to create as many rows as you wanted.

 

[project_directory]/database/seeders/AuthorSeeder.php

<?php
 
namespace Database\Seeders;
 
use App\Models\Author;
use Illuminate\Database\Seeder;
 
class AuthorSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Author::factory(5)->create();
    }
}

 

 

We are ready to run the seeder command. Run the below command:

 

php artisan migrate –seed

 

Now to get author data we need to write a route. The route file is given below:

 

[project_directory]/routes/api.php

<?php
 
use App\Http\Controllers\AuthorController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
 
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
 
Route::get('/test', function (Request $request) {
    return 'Authenticated';
});
 
 
Route::middleware('auth:api')->group(function () {
    Route::get('/user', function (Request $request) {
        return $request->user();
    });
 
Route::get('/author/{author}', [AuthorController::class, 'show'])
 
 
 
});

 

 

Go to the show function of the AuthorController.php file

 

[project_directory]/app/Http/Controllers/AuthorController.php

<?php
 
namespace App\Http\Controllers;
 
use App\Models\Author;
use App\Http\Requests\StoreAuthorRequest;
use App\Http\Requests\UpdateAuthorRequest;
 
class AuthorController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }
 
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }
 
    /**
     * Store a newly created resource in storage.
     *
     * @param  \App\Http\Requests\StoreAuthorRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreAuthorRequest $request)
    {
        //
    }
 
    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Author  $author
     * @return \Illuminate\Http\Response
     */
    public function show(Author $author)
    {
        return $author;
    }
 
    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Author  $author
     * @return \Illuminate\Http\Response
     */
    public function edit(Author $author)
    {
        //
    }
 
    /**
     * Update the specified resource in storage.
     *
     * @param  \App\Http\Requests\UpdateAuthorRequest  $request
     * @param  \App\Models\Author  $author
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateAuthorRequest $request, Author $author)
    {
        //
    }
 
    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Author  $author
     * @return \Illuminate\Http\Response
     */
    public function destroy(Author $author)
    {
        //
    }
}

 

 

This is not the right way, to make it looks formatted we can modify the show function:

 

public function show(Author $author)
    {
        return response()->json([
            'data' => [
                'id' => $author->id,
                'type' => 'Authors',
                'attributes' => [
                    'name' => $author->name,
                    'created_at' => $author->created_at,
                    'updated_at' => $author->updated_at,
                ]
            ]
        ]);
    }

 

 

It is not the final way. The best way to represent the data is to use AuthorResource. The resource can easily define how we transform the models into API resources. To generate resources we can write the make:resource artisan command. Default resources will be placed in the app/Http/Resource directory.

 

php artisan make:resource AuthorResource

 

All the codes from the show() functions need to add to the resource file. The code is given below:

 

[project_directory]/app/Http/Resources/AuthorResources.php

<?php
 
namespace App\Http\Resources;
 
use Illuminate\Http\Resources\Json\JsonResource;
 
class AuthorResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
        return [
            'data' => [
                'id' => $this->id,
                'type' => 'Authors',
                'attributes' => [
                    'name' => $this->name,
                    'created_at' => $this->created_at,
                    'updated_at' => $this->updated_at,
                ]
            ]
        ];
    }
}

 

 

After that, needs to modify the show() method in the controller

 

[project_directory]/app/Http/Controllers/AuthorController.php

use App\Http\Resources\AuthorResource;
    
public function show(Author $author)
    {
        return new AuthorResource($author);
    }

 

 

Output can be seen from the postman, like the below image: 

 

 

 

You can see the output that the id is an integer. According to the JSON API specification, the id must be a string. To do that, type the string before the id in the resource.

 

[project_directory]/app/Http/Resources/AuthorResources.php

<?php
 
namespace App\Http\Resources;
 
use Illuminate\Http\Resources\Json\JsonResource;
 
class AuthorResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
        return [
            'data' => [
                'id' => (string) $this->id,
                'type' => 'Authors',
                'attributes' => [
                    'name' => $this->name,
                    'created_at' => $this->created_at,
                    'updated_at' => $this->updated_at,
                ]
            ]
        ];
    }
}

 

 

The final output will look like this:

 

 

That's it for today. Hope this might help you in the journey of development.

 

Read More: How to Generate a Bearer Access Token Using Laravel Passport

 

K. M. Shawkat Zamil

K. M. Shawkat Zamil

Senior Software Engineer

I am a Senior Software Engineer in a reputed company in Bangladesh. I am a big fan of Laravel, PHP Programming, MSSQL Server, MySql, JavaScript, and lots more. I love to help people who are especially eager to learn. I believe in patience and motivation.