PHP code example of alcidesrh / laravel-generic-resource
1. Go to this page and download the library: Download alcidesrh/laravel-generic-resource library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
alcidesrh / laravel-generic-resource example snippets
use Alcidesrh\Generic\GenericResource;
$user = User::find(1);
// It will only return the id and name fields.
return new GenericResource( $user, ['id', 'name']);
// this will work
new GenericResource( User::find(1), ['id', 'name'] );
// this will work
new GenericResource( User::find(1), ['id', 'name', 'parent' => ['id', 'name']] );
// this will work
new GenericResource( DB::table('users')->where('id', 1)->first(), ['id', 'name'] )
// this won't
new GenericResource( DB::table('users')->where('id', 1)->first(), [
'id',
'name',
'parent' => ['id', 'name']
// it can not be access the parent property since the object retrieved is an stdClass type
] );
use Alcidesrh\Generic\GenericResourceCollection;
$users = User::where('active', 1);
// it will return a collection of user with only the id and name fields.
return new GenericResourceCollection( $users->paginate( $perPage ), ['id', 'name']);
//you can pass nested property as well as in the GenericResource
return new GenericResourceCollection( $users->paginate( $perPage ), [
'id',
'name',
'parent' => ['id', 'name'],
'products' => ['id', 'name', 'price']
]);
Method: POST /generic/list
Method: POST /generic/create
Method: POST /generic/update
Method: POST /generic/item
Method: POST /generic/delete
return [
/*
|--------------------------------------------------------------------------
| Laravel generic Resource package configuration
|--------------------------------------------------------------------------
|
*/
// configure route and prefix
// e.g. to have this route https://yourdomain/products/items for items list
// change 'prefix' key value to 'products' and 'list_route_name' key value to 'items'
'route' => [
//Route's prefix for generic CRUD(create, read, update and delete) operations
//Deafault 'generic' e.g.: axios.post( 'https://yourdomain/generic' )
'prefix' => 'generic',
//Route for list of generic items.
//Deafault 'list' e.g. axios.post( 'https://yourdomain/generic/list' )
'list_route_name' => 'list',
//Route to create an item.
//Deafault 'create' e.g. axios.post( 'https://yourdomain/generic/create', {table: 'users', values: [ {username: 'whatever', role_id: 1}], field: [id, username] } )
'create_route_name' => 'create',
//Route to update an item.
//Deafault 'update' e.g. axios.post( 'https://yourdomain/generic/update', {table: 'users', id: 1, values: [ {username: 'whatever', role_id: 1}], field: [id, username] } )
'update_route_name' => 'update',
//Route to get an item.
//Deafault 'item' e.g. axios.post( 'https://yourdomain/generic/item', {table: 'users', fields: [ {username: 'whatever', role_id: 1}] } )
'show_route_name' => 'item',
//Route to delete a generic item.
//Deafault 'delete' e.g. axios.post( 'https://yourdomain/generic/delete', {table: 'users', id: 1} )
'delete_route_name' => 'delete',
],
// configure pagination items per page and parameters names.
'pagination' => [
//Items per page. Default 20.
'itemsPerPage' => 20,
//Name of the param of the current page e.g. axios.post( 'https://yourdomain/generic/delete', {table: 'users', page: 1} )
'name_param_page' => 'page',
//Name of the param of the number of items per page e.g. axios.post( 'https://yourdomain/generic/list', {table: 'users', page: 1, itemsPerPage: 30} )
'name_param_item_per_page' => 'itemsPerPage',
],
];
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.