Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2024-07-18 13:02:22 +01:00
parent 8cfca8bff7
commit 52344c5574
2 changed files with 16 additions and 46 deletions

View file

@ -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();

View file

@ -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.
}
}