Download the PHP package alcidesrh/laravel-generic-resource without Composer
On this page you can find all versions of the php package alcidesrh/laravel-generic-resource. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download alcidesrh/laravel-generic-resource
More information about alcidesrh/laravel-generic-resource
Files in alcidesrh/laravel-generic-resource
Package laravel-generic-resource
Short Description A generic or agnostic Laravel's Resource and their CRUD(create, read, update, delete) operations.
License MIT
Informations about the package laravel-generic-resource
A generic Laravel Resource and ResourceCollection
Basic use
This package can help you to return data as a traditional Laravel Resource without making a Resource or ResourceCollection for every single Model.
Table of Contents
- Requirements
- Installation
-
Usage
- GenericResource
- GenericResourceCollection
- GenericController
- Route namespace and pagination configuration
Requirements
-Laravel >= 5
-php >= 7.0
Installation
Working with nested or related models
Supose the User class has a parent property of type User class as well, a belongsTo
relation with itself. And also has a belongsToMany
relation with Product class. So $user->parent
returns an intance of User class and $user->products
a collection of intances of Product class.
Let say we want a list of users with just these fields: id, name, parent (only the id and name fields of the parent) and products list (only the id, name and price fields of the product). This is how we can get only those data:
You can add many nested level as the relations allow:
Important: In order to return nested relations data it is required make the query through the model's Facade.
Note:
- If the second argument (the array of fields to get) is not supplied, all fields of the model will be returned.
- If one of the fields to return doesn't exist in the model, will be omitted in the result array.
GenericResourceCollection
Note: Both GenericResource
and GenericResourceCollection
classes were made following the guide line from the official Laravel's Api Resources documentation with some extra code to make it generic. So you can expect the same structure and behavior.
GenericController
The main goal of this package is to provide GenericResource
and GenericResourceCollection
. However this package also provides a GenericController
which can be used to fetch data that doesn't require a complex query or transformation, and it will return a GenericResource
or GenericResourceCollection
only with the fields that were requested or all fields if none was requested.
It can help to prevent overloading the app with routes and controller functions for every small and simple data portion required dynamically in the front-end via ajax.
The GenericController
has five routes:
/generic/list get a list. It return a GenericResourceCollection
Note: It is not posible to ask for nested relations data in the fields
parameter above due the generic nature of the query. DB Facade is used to make the query, which returns stdClass type.
/generic/create create an item. It will return a GenericResource
/generic/update update an item. It will return a GenericResource
/generic/item to get an item. It will return a GenericResource
/generic/delete delete an item
Route namespace and pagination configuration
Once installed runing console command php artisan vendor:publish
will publish the package's configuration. It can also be done manually copy /vendor/alcidesrh/generic-resource.php to /config
/config/generic-resource.php
If you find this package useful please consider star it. Thank you.
License
This Generic Resource package for Laravel is open-sourced software licensed under the MIT license