PHPackages                             mutado/laravel-resource-schema - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. mutado/laravel-resource-schema

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

mutado/laravel-resource-schema
==============================

A package for creating a schema for Laravel resources

v1.0.0(1y ago)10108[1 issues](https://github.com/mutado/laravel-resource-schema/issues)MITPHPPHP ^8.0

Since Sep 28Pushed 1y ago1 watchersCompare

[ Source](https://github.com/mutado/laravel-resource-schema)[ Packagist](https://packagist.org/packages/mutado/laravel-resource-schema)[ RSS](/packages/mutado-laravel-resource-schema/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

laravel-resource-schema
=======================

[](#laravel-resource-schema)

Laravel Resource Schema is a package that extends Laravel's API Resources, providing a flexible and powerful way to define and control the structure of your API responses.

This package was created with a huge support from [Hidden Planet Productions](https://hiddenplanetproductions.com).

Features:

- Define schema types for your resources
- Include properties dynamically
- Support for nested resources
- Optional properties that can be included on demand
- Easy to use with existing Laravel projects

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

[](#installation)

You can install the package via composer:

```
composer require mutado/laravel-resource-schema
```

Usage
-----

[](#usage)

### Basic Usage

[](#basic-usage)

1. Create a new resource that extends SchemaResource: ```
    php Mutado\LaravelResourceSchema\SchemaResource;

    class UserResource extends SchemaResource
    {
        // Define the schema types for the resource
        protected ?array $schemaTypes = [
            'mini' => [
                'id',
                'name',
            ],
            'full' => [
                'id',
                'name',
                'email',
                'posts',
                'created_at',
            ],
        ];

        protected function schema(Request $request): array
        {
            return [
                'id' => $this->id,
                'image' => fn() => ImageResource::make($this->image),
                'name' => $this->name,
                'email' => $this->email,
                // Use closure to lazy load the posts
                'posts' => fn() => PostResource::collection($this->posts),
                'created_at' => $this->created_at,
            ];
        }
    }
    ```
2. Use the resource in your controller: ```
    public function show(User $user)
    {
        // Use the schema type 'full' and include the 'image' property
        return UserResource::make($user)->useSchemaType('full')->withPartial(['image']);
    }
    ```

### Nested Resources

[](#nested-resources)

You can define how to include nested resources in your schema

```
protected ?array $schemaTypes = [
    'post' => [
        'id',
        'title',
        'content',
        // Set the schema type for the author
        'author/mini',
        // Set the schema for comments
        'comments' => [
            'id',
            'content',
            'author/mini',
        ]
    ]
];
```

### Optional Properties

[](#optional-properties)

You can define optional properties in you schema type and include them on demand using the '?' prefix:

```
// In this example we don't include the 'email' property if user is not authenticated
protected ?array $schemaTypes = [
    'profile' => [
        'id',
        'name',
        '?email',
        'posts',
        'created_at',
    ]
];

protected function show(User $user)
{
    return UserResource::make($user)
        ->useSchemaType('profile')
        // Email only included if user is authenticated
        ->withPartial([
            'email' => auth()->check(),
        ]);
}
```

### Custom Schema

[](#custom-schema)

If really need a custom schema for a specific request, you can define it using the `useSchemaType` method and by passing a schema array:

```
protected function show(User $user)
{
    return UserResource::make($user)
        ->useSchemaType([
            'id',
            'name',
            '?email',
            'posts/mini',
            'created_at',
        ]);
}
```

### Schema Property Syntax

[](#schema-property-syntax)

The Laravel Resource Schema package uses a simple but powerful syntax for defining schemas. Here's a breakdown of the schema syntax:

- `property` - A basic property is defined simply by its name
- `?property` - Optional properties are prefixed with a question mark `?`
- `nested_resource.nested_property` - You can include nested properties using dot notation
- `nested_resource[nested_property1, nested_property2]` - or array syntax
- `nested_resource.*` - To include all properties of a nested resource, use the wildcard `*`
- `nested_resource/mini` - You can specify a schema for a nested resource

Combining these syntaxes allows you to define complex and flexible schemas for your resources.

```
protected ?array $schemaTypes = [
    '?friends.avatar/mini_profile',
]
```

Contributing
------------

[](#contributing)

Contributions are welcome! Please feel free to submit a Pull Request.

License
-------

[](#license)

This package is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance35

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity44

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

Unknown

Total

1

Last Release

590d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6b30dc3809ca07010889b2e8396d41daf219b651ab2b65e67999760936c5add1?d=identicon)[mutado](/maintainers/mutado)

---

Top Contributors

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

---

Tags

laravellaravelschemalumenresource

### Embed Badge

![Health badge](/badges/mutado-laravel-resource-schema/health.svg)

```
[![Health](https://phpackages.com/badges/mutado-laravel-resource-schema/health.svg)](https://phpackages.com/packages/mutado-laravel-resource-schema)
```

###  Alternatives

[jasonlewis/resource-watcher

Simple PHP resource watcher library.

221145.8k14](/packages/jasonlewis-resource-watcher)[laracrafts/laravel-url-shortener

Powerful URL shortening tools in Laravel

97110.7k](/packages/laracrafts-laravel-url-shortener)[proai/lumen-annotations

Route and event binding annotations for Laravel Lumen

1012.4k](/packages/proai-lumen-annotations)

PHPackages © 2026

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