mirror of
https://github.com/snipe/snipe-it.git
synced 2024-11-09 23:24:06 -08:00
Better
Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
parent
8cfca8bff7
commit
52344c5574
|
@ -19,7 +19,9 @@ use League\Fractal\Serializer\DataArraySerializer;
|
|||
use League\Fractal\Serializer\ArraySerializer;
|
||||
use App\Http\Transformers\CategoriesTransformer;
|
||||
use League\Fractal\Manager;
|
||||
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
||||
use Spatie\Fractalistic\Fractal;
|
||||
use function Illuminate\Events\queueable;
|
||||
|
||||
|
||||
|
||||
|
@ -106,6 +108,16 @@ class CategoriesController extends Controller
|
|||
|
||||
|
||||
|
||||
$paginator = Category::paginate(2);
|
||||
$categories = $paginator->getCollection();
|
||||
|
||||
$results = Fractal::create()
|
||||
->collection($categories, new CategoriesTransformer())
|
||||
->serializeWith(new BootstrapTablesSerializer())
|
||||
->paginateWith(new IlluminatePaginatorAdapter($paginator))
|
||||
->toArray();
|
||||
|
||||
return $results;
|
||||
|
||||
$categories = Category::paginate(2);
|
||||
// $manager = new Manager();
|
||||
|
|
|
@ -14,7 +14,10 @@ class BootstrapTablesSerializer extends JsonApiSerializer
|
|||
public function collection($resourceKey, array $data): array
|
||||
{
|
||||
\Log::error('resource key: '.$resourceKey);
|
||||
return [$resourceKey => $data];
|
||||
return [
|
||||
'total' => count($data),
|
||||
'rows' => $data
|
||||
];
|
||||
}
|
||||
|
||||
public function item($resourceKey, array $data): array
|
||||
|
@ -25,49 +28,4 @@ class BootstrapTablesSerializer extends JsonApiSerializer
|
|||
return $data;
|
||||
}
|
||||
|
||||
public function null(): ?array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function includedData(ResourceInterface $resource, array $data): array
|
||||
{
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function meta(array $meta): array
|
||||
{
|
||||
return ['meta' => $meta];
|
||||
}
|
||||
|
||||
public function paginator(PaginatorInterface $paginator): array
|
||||
{
|
||||
$currentPage = (int) $paginator->getCurrentPage();
|
||||
$lastPage = (int) $paginator->getLastPage();
|
||||
|
||||
$pagination = [
|
||||
'total' => (int) $paginator->getTotal(),
|
||||
'count' => (int) $paginator->getCount(),
|
||||
'per_page' => (int) $paginator->getPerPage(),
|
||||
'current_page' => $currentPage,
|
||||
'total_pages' => $lastPage,
|
||||
];
|
||||
|
||||
$pagination['links'] = [];
|
||||
|
||||
if ($currentPage > 1) {
|
||||
$pagination['links']['previous'] = $paginator->getUrl($currentPage - 1);
|
||||
}
|
||||
|
||||
if ($currentPage < $lastPage) {
|
||||
$pagination['links']['next'] = $paginator->getUrl($currentPage + 1);
|
||||
}
|
||||
|
||||
return ['pagination' => $pagination];
|
||||
}
|
||||
|
||||
public function cursor(CursorInterface $cursor): array
|
||||
{
|
||||
// TODO: Implement cursor() method.
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue