2017 © Pedro Peláez
 

library laravel-restful-helper

Laravel Helper to Restful projects

image

mrjmpl3/laravel-restful-helper

Laravel Helper to Restful projects

  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 75 % Grown

The README.md

LARAVEL RESTFUL HELPER

Install

Via Composer, (*1)

``` bash $ composer require mrjmpl3/laravel-restful-helper, (*2)


## Usage This packages make queries depends of the request, like GraphQL. ### Requests - **Filter data:** /product?column=value&column2=value2 - **Sort data:** /product?sort=-column1,column2 - With the negative prefix = desc - Without the negative prefix = asc - **Fields o Select data:** /product?fields=column1,column2,column3,column4 - **Paginate and Per Page:** /product?paginate=true&per_page=5 - **Embed:** /product?embed=relationfunction ### Code #### To Collection

// Create a simple instance of model where you want apply the queries $model = new Product(); $responseHelper = new ApiRestHelper($model);, (*3)

// The method 'toCollection' return a collection with all data filtered $response = $responseHelper->toCollection();, (*4)


#### To Model

// Create a simple instance of model where you want apply the queries $model = new Product();
$responseHelper = new ApiRestHelper($model);, (*5)

// The method 'toModel' return a model with all data filtered $response = $responseHelper->toModel();, (*6)


#### From Builder to Collection

// Important! Don't close the query with get() or paginate() $query = Product::where('state', = , 1); $responseHelper = new ApiRestHelper($query);, (*7)

// The method 'toCollection' return a collection with all data filtered $response = $responseHelper->toCollection();, (*8)


#### Relations - In model, add array like next example: ``` public $apiAcceptRelations = [ 'post' ]; ``` Where 'post' is the function name of relation - In the API Resources, use the function embed ``` public function toArray($request) { $embed = (new ApiRestHelper)->getEmbed(); return [ 'id' => $this->id, 'name' => $this->name, $this->mergeWhen(array_key_exists('post', $embed), [ 'post' => $this->getPostResource($embed), ]), 'created_at' => $this->created_at, 'updated_at' => $this->updated_at, ]; } private function getPostResource($embedRequest) { $postResource = NULL; if (array_key_exists('local', $embed)) { $postRelation = $this->local(); $fieldsFromEmbed = (new ApiRestHelper($postRelation->getModel()))->getEmbedField('post'); if(!empty($fieldsFromEmbed)) { $postResource = new PostResource($postRelation->select($fieldsFromEmbed)->first()); } else { $postResource = new PostResource($postRelation->first()); } } return $postResource; } ``` #### Transformers - In model, add array like next example:

public $apiTransforms = [ 'id' => 'code' ];, (*9)


Where 'id' is the db column name , and 'code' is the column rename to response - In the API Resources, use the array $apiTransforms

$apiHelper = new ApiRestHelper($this);, (*10)

return [ $apiHelper->getKeyTransformed('id') => $this->id, 'name' => $this->name, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at, ];, (*11)


- To used fields in API Resources , You can combine with transformers fields

$apiHelper = new ApiRestHelper($this);, (*12)

return [ $this->mergeWhen($apiHelper->existInFields('id') && !is_null($this->id), [ $this->transforms['id'] => $this->id ]), $this->mergeWhen($apiHelper->existInFields('name') && !is_null($this->name), [ 'name' => $this->name ]), ], (*13)


#### Exclude Fields in Filter - In model, add array like next example:

public $apiExcludeFilter = [ 'id' ]; ```, (*14)

Where 'id' is the db column name to exclude, (*15)

Change log

Please see CHANGELOG for more information on what has changed recently., (*16)

Security

If you discover any security related issues, please email jmpl3.soporte@gmail.com instead of using the issue tracker., (*17)

License

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

The Versions

03/05 2018

dev-master

9999999-dev https://github.com/MrJmpl3/Laravel_Restful_Helper

Laravel Helper to Restful projects

  Sources   Download

MIT

The Requires

 

laravel helper restful mrjmpl3 laravel_restful_helper

03/05 2018

1.6.0

1.6.0.0 https://github.com/MrJmpl3/Laravel_Restful_Helper

Laravel Helper to Restful projects

  Sources   Download

MIT

The Requires

 

laravel helper restful mrjmpl3 laravel_restful_helper

03/05 2018

dev-develop

dev-develop https://github.com/MrJmpl3/Laravel_Restful_Helper

Laravel Helper to Restful projects

  Sources   Download

MIT

The Requires

 

laravel helper restful mrjmpl3 laravel_restful_helper