PHP code example of mrjmpl3 / laravel-restful-helper

1. Go to this page and download the library: Download mrjmpl3/laravel-restful-helper 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/ */

    

mrjmpl3 / laravel-restful-helper example snippets


    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;
    }
    

$apiHelper = new ApiRestHelper($this);

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

$apiHelper = new ApiRestHelper($this);

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
    ]),
]