PHPackages                             adelynx/laravel-api-resources - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. adelynx/laravel-api-resources

ActiveLibrary[API Development](/categories/api)

adelynx/laravel-api-resources
=============================

Manage your resources maintaining API versioning

v1.0.1(7y ago)24MITPHPPHP ^7.1.3

Since Dec 25Pushed 7y ago1 watchersCompare

[ Source](https://github.com/adelynx/laravel-api-resources)[ Packagist](https://packagist.org/packages/adelynx/laravel-api-resources)[ Docs](https://github.com/adelynx/laravel-api-resources)[ RSS](/packages/adelynx-laravel-api-resources/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (2)Dependencies (4)Versions (3)Used By (0)

Laravel API Resources
=====================

[](#laravel-api-resources)

[![Latest Version](https://camo.githubusercontent.com/3b87bac43c51ef19e51c358685a4abd1f91f80dc78defe85a8fc5fe0dea9ea1b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f6164656c796e782f6c61726176656c2d6170692d7265736f75726365732e7376673f7374796c653d666c61742d737175617265)](https://github.com/adelynx/laravel-api-resources/releases)[![Build Status](https://camo.githubusercontent.com/0beb64e2248a62bd456888252588c011cb7fd254e96917e3aff02808583fe866/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6164656c796e782f6c61726176656c2d6170692d7265736f75726365732f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/adelynx/laravel-api-resources)[![Total Downloads](https://camo.githubusercontent.com/0d68e3b691c28e65786563c3eeccaf21a7a85321d53d58c42f779d6560e89ab4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6164656c796e782f6c61726176656c2d6170692d7265736f75726365732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/adelynx/laravel-api-resources)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)

Manage your resources maintaining API versioning. With a simple middleware separate routes by API version, and smart instanciate [Http\\Resources](https://laravel.com/docs/5.7/eloquent-resources) based on this version.

Add the middleware `'api.v:2'` on your api/v2 group.

And then `api_resource('App\User')->make($user)` is the same as `new App\Http\Resources\App\v2\User($user)`, but version free.

```
App\Http\Resources\
  |- App\
    |- v1\
      |- User.php
    |- v2\
      |- Rank.php
      |- User.php
```

Installation
------------

[](#installation)

You can install this package via composer using:

```
composer require adelynx/laravel-api-resources
```

The package will automatically register itself.

### Config

[](#config)

To publish the config file to `config/api.php` run:

```
php artisan vendor:publish --provider="Adelynx\APIResources\APIResourcesServiceProvider"
```

This will publish a file `api.php` in your config directory with the following content:

```
return [
  /*
  |--------------------------------------------------------------------------
  | API Version
  |--------------------------------------------------------------------------
  |
  | This value is the latest version of your api. This is used when
  | there's no specified version on the routes, so it will take this as the
  | default, or latest.
   */
   'version' => '1',

   /*
   |--------------------------------------------------------------------------
   | Resources home path
   |--------------------------------------------------------------------------
   |
   | This value is the base folder where your resources are stored.
   | When using multiple APIs, you can leave it as a string if every
   | api is in the same folder, or as an array with the APIs as keys.
    */
    'resources_path' => 'App\Http\Resources',

    /*
    |--------------------------------------------------------------------------
    | Resources
    |--------------------------------------------------------------------------
    |
    | Here is the folder that has versioned resources. If you store them
    | in the root of 'resources_path', leave this empty or null.
     */
    'resources' => 'App'
 ];
```

### Middleware

[](#middleware)

Install this middleware on your `Http/Kernel.php` under the `$routeMiddleware`

```
  protected $routeMiddleware = [
    ...
    'api.v'           => \Adelynx\APIResources\Middleware\APIversion::class,
    ...
  ];
```

Configure correctly
-------------------

[](#configure-correctly)

For this package to work, you need to understand how it requires resources.

If we have the following config:

```
[
  'version' => '2',
  'resources_path' => 'App\Http\Resources',
  'resources' => 'Api'
]
```

This means that if you include the `Api\User` resource, it will instantiate `App\Http\Resources\Api\v2\User`.

`Api` works for sub organizing your structure, but you can put your Resources versioned folders in the root, like this:

```
[
  'version' => '2',
  'resources_path' => 'App\Http\Resources',
  'resources' => ''
]
```

Now if we include `User`, it will instantiate `App\Http\Resources\v2\User`.

### Fallback

[](#fallback)

When you use a version that is **NOT** the latest, if you try to include a Resource that's **NOT** defined inside that version's directory, this will automatically fallback in the **LATEST** version.

This way you don't have to duplicate new resources on previous versions.

Usage
-----

[](#usage)

### Middleware

[](#middleware-1)

When you group your API routes, you should now apply the middleware `api.v` into the group like this:

```
// App v1 API
Route::group([
    'middleware' => ['app', 'api.v:1'],
    'prefix'     => 'api/v1',
], function ($router) {
    require base_path('routes/app_api.v1.php');
});

// App v2 API
Route::group([
    'middleware' => ['app', 'api.v:2'],
    'prefix'     => 'api/v2',
], function ($router) {
    require base_path('routes/app_api.v2.php');
});
```

That way, if you use the Facade, you can check the current version by doing `APIResource::getVersion()` and will return the version specified on the middleware.

### Facade

[](#facade)

There are many ways to create resources. You can use the Facade accessor:

```
use Adelynx\APIResources\Facades\APIResource;

class SomethingController extends Controller {
    ...

    public function show(Something $model)
    {
      return APIResource::resolve('App\Something')->make($model);
    }
}
```

### Global helper

[](#global-helper)

```
class SomethingController extends Controller {
    ...

    public function show(Something $model)
    {
      return api_resource('App\Something')->make($model);
    }
}
```

### Collections

[](#collections)

Instead of `make`, use `collection` for arrays, just like Laravel's documentation.

```
class SomethingController extends Controller {
    ...

    public function index()
    {
      $models = Something::all();
      return api_resource('App\Something')->collection($models);
    }
}
```

Nested resources
----------------

[](#nested-resources)

To take advantage of the **fallback** functionality, it's recommended to use `api_resource` inside the resources. This way you preserve the right version, or the latest if it's not defined.

```
class Post extends Resource {
    public function toArray($request)
    {
      return [
        'title' => $this->title,
          ...
        'user' => api_resource('App\User')->make($this->user);
      ];
    }
}
```

Multiple APIs
-------------

[](#multiple-apis)

There might be the case where you have more than one API living on the same project, but using different versions. This app supports that. First, the `config/api.php`

```
return [
  'default' => 'api',
  'version' => [
    'api'     => '2',
    'desktop' => '3'
  ],
  'resources_path' => 'App\Http\Resources'
  // Or one path each
  'resources_path' => [
    'api'     => 'App\Http\Resources',
    'desktop' => 'Vendorname\ExternalPackage\Resources'
  ],
  'resources' => [
    'api'     => 'Api',
    'desktop' => ''
  ],
];
```

Then, you need to configure the **middleware**. Instead of using `api.v:1`, you now have to specify the name: `api.v:3,desktop`.

Then the rest works as explained before.

Testing
-------

[](#testing)

Run the tests with:

```
vendor/bin/phpunit
```

Credits
-------

[](#credits)

- [Adel KEDJOUR](https://github.com/adelynx)
- [Juan Pablo Barreto](https://github.com/juampi92)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE) for more information.

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

2691d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/26a26bbcf45bad0a3e5e97cdc1afd98b435f64b878a9d17aac161d4e0239a4d4?d=identicon)[adelynx](/maintainers/adelynx)

---

Top Contributors

[![adelynx](https://avatars.githubusercontent.com/u/496591?v=4)](https://github.com/adelynx "adelynx (4 commits)")

---

Tags

apilaravel

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/adelynx-laravel-api-resources/health.svg)

```
[![Health](https://phpackages.com/badges/adelynx-laravel-api-resources/health.svg)](https://phpackages.com/packages/adelynx-laravel-api-resources)
```

###  Alternatives

[essa/api-tool-kit

set of tools to build an api with laravel

52680.5k](/packages/essa-api-tool-kit)[resend/resend-laravel

Resend for Laravel

1191.4M6](/packages/resend-resend-laravel)[mollie/laravel-mollie

Mollie API client wrapper for Laravel &amp; Mollie Connect provider for Laravel Socialite

3624.1M28](/packages/mollie-laravel-mollie)[dragon-code/laravel-json-response

Automatically always return a response in JSON format

1118.6k1](/packages/dragon-code-laravel-json-response)[surface/laravel-webfinger

A Laravel package to create an ActivityPub webfinger.

113.8k](/packages/surface-laravel-webfinger)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
