GithubHelp home page GithubHelp logo

payamowow / laravel-resources Goto Github PK

View Code? Open in Web Editor NEW

This project forked from owowagency/laravel-resources

0.0 0.0 0.0 111 KB

A package to create projects faster

License: Other

PHP 100.00%

laravel-resources's Introduction

Laravel Resources

Create api endpoints with ease.

Route registration

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.

Example:

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' => [],

];

laravel-resources's People

Contributors

thomasowow avatar dees040 avatar thomas-veen avatar mindabartosz avatar dependabot-preview[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.