示例#1
0
 /**
  * Manage and finalize the data transformation.
  *
  * @param  \League\Fractal\Resource\Item|\League\Fractal\Resource\Collection  $data
  * @param  int  $code
  * @param  array  $meta
  * @return \Illuminate\Http\JsonResponse
  */
 public function transform($data, $code = 200, $meta = [])
 {
     $data->setMeta($meta);
     $manager = new Manager();
     $manager->setSerializer(new DataArraySerializer());
     $response = $manager->createData($data)->toArray();
     return response()->json($response, 200, [], JSON_UNESCAPED_SLASHES);
 }
示例#2
0
 /**
  * Create FractalCollection payload with pagination.
  *
  * @param \Illuminate\Contracts\Pagination\LengthAwarePaginator $paginator
  * @param null                                                  $transformer
  * @param string|null                                           $resourceKey
  * @return mixed
  */
 public function getPagination(LengthAwarePaginator $paginator, $transformer = null, $resourceKey = null)
 {
     // Append existing query parameter to pagination link
     // @see http://fractal.thephpleague.com/pagination/#including-existing-query-string-values-in-pagination-links
     $queryParams = array_diff_key($_GET, array_flip(['page']));
     foreach ($queryParams as $key => $value) {
         $paginator->addQuery($key, $value);
     }
     $collection = $paginator->getCollection();
     $resource = new FractalCollection($collection, $this->getTransformer($transformer), $this->getResourceKey($resourceKey));
     $resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
     if ($meta = $this->getMeta()) {
         $resource->setMeta($meta);
         $this->meta = [];
     }
     if ($includes = $this->getIncludes()) {
         $this->fractal->parseIncludes($this->request->input('include'));
         $this->includes = null;
     }
     return $this->fractal->createData($resource)->toArray();
 }