New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

grantholle/api-resource-detection

Detect API Resources for your Eloquent models.
1,177 6
Install
composer require grantholle/api-resource-detection
Latest Version:1.4.0
Laravel:6 – 12
License:MIT
Last Updated:Feb 25, 2025
Links: GitHub  ·  Packagist
Maintainer: grantholle

API Resource Detection

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

Installation

composer require grantholle/api-resource-detection 

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';
    });
}

Related Packages

doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

648,266,899 9,701
laravel/framework

The Laravel Framework.

604,216,237 34,982
laravel/tinker

Powerful REPL for the Laravel framework.

512,508,631 7,443
laravel/serializable-closure

Laravel Serializable Closure provides an easy and secure way to serialize closur...

429,221,767 607
nunomaduro/collision

Cli error handling for console/command-line PHP applications.

403,217,078 4,664

Version History

Version Released Laravel License
1.4.0 ^6.0|^7.0|^8.0|^9.0|^10.0|^11.0|^12.0 MIT
1.3.0 ^6.0|^7.0|^8.0|^9.0|^10.0|^11.0 MIT
1.2.0 ^6.0|^7.0|^8.0|^9.0|^10.0 MIT
1.1.0 ^6.0|^7.0|^8.0|^9.0 MIT
1.0.0 ^6.0|^7.0|^8.0 MIT