PHPackages                             faliure/laravel-resourceable - 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. faliure/laravel-resourceable

ActiveLibrary

faliure/laravel-resourceable
============================

Access a Model's underlying Resources from the Model itself

1.0.1(3y ago)064MITPHP

Since Feb 4Pushed 3y ago1 watchersCompare

[ Source](https://github.com/faliure/laravel-resourceable)[ Packagist](https://packagist.org/packages/faliure/laravel-resourceable)[ RSS](/packages/faliure-laravel-resourceable/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (3)Used By (0)

faliure/laravel-resourceable
============================

[](#faliurelaravel-resourceable)

Laravel Resourceable allows you to set a clear relationship between an Eloquent Model and its associated Resource(s): **a Model owns its Resources**.

As of Laravel 9.5, this is still not obvious. There is indeed a default path to store the main Model's Resource, but you still need to do things like `new UserResource($user)`. That is, you're just creating an instance of a certain resource, and passing the model as an argument. You can even pass some other type of Model, or even something that's not an Eloquent Model (within reason) and everything will work.

With this package, we make the relation explicit and change the code to reflect that a JsonResource of a Model belongs to that Model, not the other way around.

---

Usage
-----

[](#usage)

Use the `HasResources` trait and implement the `Resourceable` interface in your Models.

```
# App\Models\User

use Faliure\Resourceable\Contracts\Resourceable;
use Faliure\Resourceable\Traits\HasResources;

class User implements Resourceable
{
    use HasResources;
}
```

Get a single Model's associated Resource:

```
$user = User::first();

// Without Resourceables
$resource = new UserResource($user);

// With Resourceables
$resource = $user->resource();
```

Get all of the model's resources:

```
// Without Resourceables
$users = User::all();
$resources = UserResource::collection($user);

// With Resourceables
$resources = User::resources();
```

Get a custom Collection of resources of a given Model:

```
// Without Resourceables
$users = User::where('some', 'constraint')->get();
$resources = UserResource::collection($users);

// With Resourceables
$resources = User::resourcesQuery()->where('some', 'constraint')->get();
```

As you can see, this syntactic sugar puts the Model in the spotlight, and the resource as one of its many associated entities.

---

Customization
-------------

[](#customization)

By default, Resourceable assumes your main Model JsonResource follows the standard:

- User's main Resource is `App\Http\Resources\UserModel`

If that's not the case, you may define a property `resourceClass` in your Resourceable Modem, like so:

```
class User implements Resourceable
{
    // ...

    protected string $resourceClass = TheResourceClass::class;
}
```

Moreover, you can fetch different kinds of Resources for a model, by passing an optinal `$resourceClass` parameter to any of the methods defined in the interface. Namely:

```
$resource = $user->resource(SomeOtherResourceType::class);

$resources = User::resources(SomeOtherResourceType::class);

$filteredResources = User::resourcesQuery(SomeOtherResourceType::class)
    ->where('some', 'constraint')
    // ->...
    ->get();
```

It's just as simple as that!

### How does the `resourcesBuilder` work?

[](#how-does-the-resourcesbuilder-work)

The `resourcesBuilder` is a Custom Eloquent Builder that allows you to write Eloquent chains as you normally would, but it modifies the end result to return the associated Resources instead of a Model or a collection of them.

Check [faliure/laravel-custom-builder](https://github.com/faliure/laravel-custom-builder) for more information and other usages.

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 50% 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 ~9 days

Total

2

Last Release

1181d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2d47a0714c283109f82f0ddb6d34c8898408db5180c61bca65d72452b737c404?d=identicon)[faliure](/maintainers/faliure)

---

Top Contributors

[![dbarreiro](https://avatars.githubusercontent.com/u/462973?v=4)](https://github.com/dbarreiro "dbarreiro (4 commits)")[![faliure](https://avatars.githubusercontent.com/u/714232?v=4)](https://github.com/faliure "faliure (4 commits)")

### Embed Badge

![Health badge](/badges/faliure-laravel-resourceable/health.svg)

```
[![Health](https://phpackages.com/badges/faliure-laravel-resourceable/health.svg)](https://phpackages.com/packages/faliure-laravel-resourceable)
```

PHPackages © 2026

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