New

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

owowagency/laravel-resources

A package to develop projects faster.
12,073 11
Install
composer require owowagency/laravel-resources
Latest Version:v3.2.5
Laravel:10 · 11 · 12
Older releases:5 – 9
License:MIT
Last Updated:Feb 4, 2025
Links: GitHub  ·  Packagist
Maintainer: dees040

banner-dark banner-light


Sick and tired of having to create the same controller over and over?
Look no further! OWOW presents "Laravel Resources".
A package that makes creating API endpoints feel like breeze.

📖 Table of contents

  1. Installation
  2. Usage
  3. Contributing
  4. License
  5. OWOW

⚙️ Installation

composer require owowagency/laravel-resources

🛠 Usage

This package adds a few new features to the already existing apiResource method, model and requests.

model will be used to determine what model is being be handled.
requests should contain the form requests that are used to validate incoming data during creation and updating.

Route registration

use App\Http\Requests\Posts\StoreRequest;
use App\Http\Requests\Posts\UpdateRequest;
use App\Models\Post;
use OwowAgency\LaravelResources\Controllers\ResourceController;

Route:apiResource(
    'posts',
    ResourceController::class,
    [
        'model' => Post::class,
        'requests' => [
            'store' => StoreRequest::class,
            'update' => UpdateRequest::class,
        ],
    ],
);

Customizing controllers

All methods in the ResourceController can be overwritten. We made it a little easier by adding methods like indexModel, and updateModel. You will not have to worry about validating, authorization, or returning the models as response.

use OwowAgency\LaravelResources\Controllers\ResourceController;

class PostController extends ResourceController
{
    /**
     * Returns models instances used for the index action.
     * 
     * @return mixed
     */
    public function indexModel()
    {
        return Post::where('title', 'LIKE', request('search'))->paginate();
    }

    /**
     * Updates and returns the model instance for the update action.
     * 
     * @param  \Illuminate\Http\Request  $request
     * @param  \Illuminate\Database\Eloquent\Model  $model
     * @return void
     */
    public function updateModel(Request $request, Model $model)
    {
        $model->update(['user_id' => \Auth::user()->id]);
    }
}

Eloquent API resources

This package will always try to return the API resource representation of the specified model. It applies auto discovery to determine what resource to use.

By default it will use the following pattern to discover the resource class:

App\Http\Resources\{class_baseName($modelClass)}Resource

In case of a Post model that will become:

App\Http\Resources\PostResource

Configuration

Configuration can be published with:

php artisan vendor:publish --tag=laravelresources
return [

    /**
     * Is used for auto discovery of http resources. Allows for placing
     * resources under a different namespace.
     */
    'resource_namespace' => 'App\\Http\\Resources',

    /**
     * Configure resources that do not follow the default auto discovery rules.
     * 
     * Eg:
     * [Post::class => SpecialPostResource::class]
     */
    'resource_factory' => [],

];

🫶 Contributing

Please see CONTRIBUTING for details.

📜 License

The MIT License (MIT). Please see License File for more information.



This package has been brought to you with much love by the wizkids of OWOW. Do you like this package? We’re still looking for new talent and Wizkids. So do you want to contribute to open source, while getting paid? Apply now

Related Packages

kraenzle-ritter/resources

Laravel package for managing external resources with Livewire components

517 0
alibori/laravel-api-resource-generator

Package to generate API resources from models.

2,884 21
balismatz/filament-prevent-outdated-record-update

Filament package to prevent the update of outdated records.

13,373 10
farbcode/laravel-stateful-resources

A Laravel package for managing state in API resources.

3,662 5
baethon/laravel-resource

Conveniant factory for Laravel API resources

1,297 4

Version History

Version Released Laravel License
v3.2.5 ^10.0|^11.0|^12.0 MIT
v3.2.4 ^10.0|^11.0|^12.0 MIT
v3.2.3 ^10.0 MIT
v3.2.2 ^10.0 MIT
v3.2.1 ^10.0 MIT