PHPackages                             dgoring/laravel-inherit-resource - 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. dgoring/laravel-inherit-resource

ActiveLibrary[Framework](/categories/framework)

dgoring/laravel-inherit-resource
================================

Quick minilist resource controllers for Laravel

4.3.2(5mo ago)1977MITPHPPHP ^7.1.3|^8.0CI failing

Since May 28Pushed 5mo ago1 watchersCompare

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

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

Laravel Inherit Resource
========================

[](#laravel-inherit-resource)

Inspired by the ruby gem InheritResource

I bring you quick minimalist resource controllers in laravel

Basic usage
-----------

[](#basic-usage)

Simply place resource trait in your controller

```
namespace App\Http\Controllers;

use Dgoring\Laravel\InheritResource\Resource;

class UsersController extends Controller
{
  use Resource;
}
```

with the resource route in your routes file

```
Route::resource('users', 'UsersController');
```

And all the resource functions are placed and run against the class as well as loading the views

all settings are assumed by the name of the controller so `UsersController` is assumed to use

- `App\User` for the model class
- `user` the route parameter
- these views are assumed for there respective resource functions -- `users.index` with `$users` passed into it as a paginator collection -- `users.show` with `$user` passed into it as the instance of the record -- `users.create` with `$user` a fresh new instance of the model -- `users.edit` with `$user` passed into it as the instance of the record

the query
---------

[](#the-query)

To override the query used for say filtering the index page you can override the `collection` function

```
class UsersController extends Controller
{
  use Resource { collection as query; }

  protected function collection()
  {
    $query = $this->query();

    if($search = request()->query('search'))
    {
      $query->where('name', 'like', "%{$search}%");
    }

    return $query;
  }
}
```

The `collection` function is also used for all resource functions to give the base query So can be used to filter content across the controller

```
  protected function collection()
  {
    $query = $this->query();

    $query->where('active', 1);

    return $query;
  }
```

Authorized Resource
-------------------

[](#authorized-resource)

By default all the actions are put through the authorize function, so you can control access to this resource

- `index` -&gt;authorize(`viewAny`, class)
- `create` -&gt;authorize(`create`, new instance)
- `store` -&gt;authorize(`create`, new instance)
- `edit` -&gt;authorize(`update`, instance)
- `update` -&gt;authorize(`update`, instance)
- `destroy` -&gt;authorize(`delete`, instance)

Validation rules
----------------

[](#validation-rules)

You can specify validation rules for the `store` and `update` functions but defining `validationRules` function

```
class UsersController extends Controller
{
  use Resource;

  protected function validationRules()
  {
    if($this->resource()->exists)
    {
      return [
        'name' => 'string',
      ];
    }
    else
    {
      return [
        'name' => 'string|required',
      ];
    }
  }
}
```

nested resource
---------------

[](#nested-resource)

Nested resources are just as easy

Simply setup your route

```
Route::resource('teams.users', 'UsersController');
```

And return the relationship in the collection function

```
use App\Team;

class UsersController extends Controller
{
  use Resource { collection as query; }

  protected function collection()
  {
    $team = Team::findOrFail(request()->route('team'));

    $query = $team->users();

    if($search = request()->query('search'))
    {
      $query->where('name', 'like', "%{$search}%");
    }

    return $query;
  }
}
```

as the collection function is used for the `create` and `store` functions as long as your return the relationship from the parent model the relationship should be saved to the new record and it should also already be in the model in the `create` view variable

JSON
----

[](#json)

this resource is also ready to respond to json requests note that the index will return a total count in the response headers as `Count`

also if defined in a appropriate place a JsonCollection or JsonResource formatter will be used for the model i.e.

```
namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class UserResource extends JsonResource
{

}

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;

class UsersCollection extends ResourceCollection
{

}
```

And you can also allow only JSON responses

```
namespace App\Http\Controllers;

use Dgoring\Laravel\InheritResource\JsonResource;

class UsersController extends Controller
{
  use JsonResource;
}
```

or only HTML

```
namespace App\Http\Controllers;

use Dgoring\Laravel\InheritResource\HtmlResource;

class UsersController extends Controller
{
  use HtmlResource;
}
```

Overrides
---------

[](#overrides)

### Namespaces

[](#namespaces)

By default models are assumed to be under the `App` namespace but you can change that by adding the file `config/inherit_resource.php`

below shows the config file for `App\Models`

and also the namespaces for the json resources and collections

```
