PHPackages                             morningtrain/laravel-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. [Framework](/categories/framework)
4. /
5. morningtrain/laravel-resources

ActiveLibrary[Framework](/categories/framework)

morningtrain/laravel-resources
==============================

Resources system for Laravel

4.25.0(3y ago)03.0k2GPL-3.0-onlyPHPPHP ^7.4|^8.0|^8.1

Since Mar 22Pushed 3y ago3 watchersCompare

[ Source](https://github.com/Morning-Train/LaravelResources)[ Packagist](https://packagist.org/packages/morningtrain/laravel-resources)[ RSS](/packages/morningtrain-laravel-resources/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (12)Versions (127)Used By (2)

Resources and Operations for Laravel
====================================

[](#resources-and-operations-for-laravel)

Operations are a set of actions on the server that handles all logic from setting up routes to handling client requests.

A simple operation could be an index operation for getting all users and returning them to the client.

Another use-case could be triggering an action on an Eloquent model. When doing this, one would always have to validate user input, fetch the model, execute the method and then perhaps return something meaningful to the user.

This is similar to a simple operation where we need to return a single instance of a model to the user (a traditional read call) - but it contains some additional logic to handle method execution.

A delete operation is similar, in that it first fetches the model and the trigger the delete method on it.

Install
-------

[](#install)

Via Composer

```
$ composer require morningtrain/laravel-resources
$ php artisan vendor:publish --provider="MorningTrain\Laravel\Resources\LaravelResourcesServiceProvider"
```

Configuration
-------------

[](#configuration)

Register your resources and operations in `config/resources.php`:

The first level key corresponds to namespace, and value is an array of your resource classes. You can give resources custom names with array keys, and nest arrays of resources. Just make sure all items have unique keys.

If no key is provided for a resource, the `Str::snake(class_basename($resource))` is used. This means if you have a class called `User` and another resource with the key `"user"` in the same namespace, they will collide.

Example:

```
'api' => [
    \App\Operations\Api\User::class,
    'custom_user' => \App\Operations\Api\User::class,

    'nested' => [
        \App\Operations\Api\User::class,
        'custom_user' => \App\Operations\Api\User::class,

        'deep_nested' => [
            \App\Operations\Api\User::class,
            'custom_user' => \App\Operations\Api\User::class,
        ],

    /*
     * These two will not work together - non-unique resource name:
     *  \App\Operations\Api\MyResource::class,
     *  'my_resource' => \App\Operations\Api\User::class,
     */
],
```

Examples
--------

[](#examples)

### Index Operation

[](#index-operation)

The purpose of the `Index` operation is to retrieve a list of model entries and return the list as JSON.

This code example is taken from the package and will illustrate how the Index operation is implemented.

```
