Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
8 contributors

Users who have contributed to this file

@taylorotwell @driesvints @themsaid @michaeldyrynda @lucasmichot @laurencei @mnabialek @dbpolito
75 lines (64 sloc) 1.65 KB
<?php
namespace Illuminate\Http\Resources\Json;
use Countable;
use IteratorAggregate;
use Illuminate\Pagination\AbstractPaginator;
use Illuminate\Http\Resources\CollectsResources;
class ResourceCollection extends JsonResource implements Countable, IteratorAggregate
{
use CollectsResources;
/**
* The resource that this resource collects.
*
* @var string
*/
public $collects;
/**
* The mapped collection instance.
*
* @var \Illuminate\Support\Collection
*/
public $collection;
/**
* Create a new resource instance.
*
* @param mixed $resource
* @return void
*/
public function __construct($resource)
{
parent::__construct($resource);
$this->resource = $this->collectResource($resource);
}
/**
* Return the count of items in the resource collection.
*
* @return int
*/
public function count()
{
return $this->collection->count();
}
/**
* Transform the resource into a JSON array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return $this->collection->map->toArray($request)->all();
}
/**
* Create an HTTP response that represents the object.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function toResponse($request)
{
return $this->resource instanceof AbstractPaginator
? (new PaginatedResourceResponse($this))->toResponse($request)
: parent::toResponse($request);
}
}
You can’t perform that action at this time.