PHPackages                             grantholle/api-resource-detection - 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. grantholle/api-resource-detection

ActiveLibrary[API Development](/categories/api)

grantholle/api-resource-detection
=================================

Detect API Resources for your Eloquent models.

1.4.0(1y ago)61.2kMITPHP

Since Dec 15Pushed 1y ago1 watchersCompare

[ Source](https://github.com/grantholle/api-resource-detection)[ Packagist](https://packagist.org/packages/grantholle/api-resource-detection)[ RSS](/packages/grantholle-api-resource-detection/feed)WikiDiscussions main Synced today

READMEChangelog (1)Dependencies (3)Versions (6)Used By (0)

API Resource Detection
======================

[](#api-resource-detection)

This package provides a trait to add to your Eloquent models to get automatic API Resource detection.

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

[](#installation)

```
composer require grantholle/api-resource-detection

```

Usage
-----

[](#usage)

You generate resources for your models by using the `php artisan make:resource` command. This will create a resource in your `app/Http/Resources` directory. This package detects different naming conventions. You can name your resources the same as your model or name it using the `Resource` suffix, like `ModelResource` In your models that have resources, you can add the `HasResource` trait.

Assume we have a resource named `UserResource`.

```
php artisan make:resource UserResource

```

Let's add the trait to the `User` model:

```
namespace App\Models;

use GrantHolle\Http\Resources\Traits\HasResource;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use HasResource;

    protected $guarded = [];
}
```

Now when you want to get the resource for the model, you can call `toResource()` on the model instance to detect and retrieve the API resource for the model.

```
$user = User::find(1);

// This will be a UserResource instance
$user->toResource();
```

You can also use the alternative syntax of `Model::resource()`.

```
$user = User::find(1);

User::resource($user);
```

This also works for collection of models.

```
$users = User::all();

User::resource($users);
```

This will automatically look for a `UserCollection` or `UserCollectionResource` resource class. If none is found it will default to calling the `collection()` function on the standard resource class.

If your application has a different namespace other than the default resource namespace, you can tell it to use a different namespace for your resources.

In a service provider,

```
public function boot() {
    \GrantHolle\Http\Resources\JsonResource::resolveResourceNamespaceUsing(function () {
        return 'My\\Resource\\Namespace\\';
    });
}
```

You can also change how the resources is guessed and return a new class name based on the model. By default, it looks for your model name or model name with the `Resource` suffix in the default resource namespace. You can also customize how collections are resolved.

```
public function boot() {
    \GrantHolle\Http\Resources\JsonResource::guessResourceNamesUsing(function ($modelClass) {
        $modelName = class_basename($modelClass);

        // This will return a resource with the class name
        // ModelApiResource in the `My\Resource\Namespace` namespace.
        return 'My\\Resource\\Namespace\\' . $modelName . 'ApiResource';
    });

    \GrantHolle\Http\Resources\JsonResource::guessResourceCollectionNamesUsing(function ($modelClass) {
        $modelName = class_basename($modelClass);

        // This will return a resource with the class name
        // ModelCollectionApiResource in the `My\Resource\Namespace` namespace.
        return 'My\\Resource\\Namespace\\' . $modelName . 'CollectionApiResource';
    });
}
```

###  Health Score

34

—

LowBetter than 75% of packages

Maintenance41

Moderate activity, may be stable

Popularity19

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity57

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 ~383 days

Total

5

Last Release

492d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/57ed974235b4a23e6aaf9d9039bff2b0d1268edc0e44ebab6e60e4bf1e6eb144?d=identicon)[grantholle](/maintainers/grantholle)

---

Top Contributors

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

### Embed Badge

![Health badge](/badges/grantholle-api-resource-detection/health.svg)

```
[![Health](https://phpackages.com/badges/grantholle-api-resource-detection/health.svg)](https://phpackages.com/packages/grantholle-api-resource-detection)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M346](/packages/psalm-plugin-laravel)[mike-bronner/laravel-model-caching

Automatic caching for Eloquent models.

2.4k90.5k1](/packages/mike-bronner-laravel-model-caching)[api-platform/laravel

API Platform support for Laravel

58171.4k14](/packages/api-platform-laravel)[flat3/lodata

OData v4.01 Producer for Laravel

99351.7k](/packages/flat3-lodata)[simplestats-io/laravel-client

Server-side analytics for Laravel that follows the full funnel from visit to registration to payment, attributed to the channel that drove it. Revenue, MRR, churn and ad-spend profit (ROAS/CAC) per channel. GDPR compliant, ad-blocker proof.

5021.9k](/packages/simplestats-io-laravel-client)[pressbooks/pressbooks

Pressbooks is an open source book publishing tool built on a WordPress multisite platform. Pressbooks outputs books in multiple formats, including PDF, EPUB, web, and a variety of XML flavours, using a theming/templating system, driven by CSS.

45444.2k1](/packages/pressbooks-pressbooks)

PHPackages © 2026

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