Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2024-07-18 12:56:28 +01:00
parent f400b38c9c
commit 8cfca8bff7
2 changed files with 79 additions and 19 deletions

View file

@ -13,11 +13,14 @@ use Illuminate\Support\Facades\Storage;
use Illuminate\Pagination\LengthAwarePaginator;
use App\Models\Traits\ApiResponder;
use App\Http\Serializers\BootstrapTablesSerializer;
use League\Fractal\Manager;
use League\Fractal\Resource\Item;
use League\Fractal\Resource\Collection;
use League\Fractal\Serializer\DataArraySerializer;
use League\Fractal\Serializer\ArraySerializer;
use App\Http\Transformers\CategoriesTransformer;
use League\Fractal\Manager;
use Spatie\Fractalistic\Fractal;
class CategoriesController extends Controller
@ -31,7 +34,7 @@ class CategoriesController extends Controller
* @since [v4.0]
* @return \Illuminate\Http\Response
*/
public function index(Request $request) : array
public function index(Request $request)
{
$this->authorize('view', Category::class);
$allowed_columns = [
@ -102,17 +105,29 @@ class CategoriesController extends Controller
}
//$manager = new Manager();
//$manager->setSerializer(new BootstrapTablesSerializer());
$categories = Category::latest()->paginate(2);
$transformer = $categories->first()->transformer;
$resource = new Collection($categories, new CategoriesTransformer(), 'rows');
return $categories->transformWith(new CategoriesTransformer('rows'))->toArray();
return new Collection('rows', $categories->toArray(), new CategoriesTransformer());
$categories = Category::paginate(2);
// $manager = new Manager();
// $manager->setSerializer(new BootstrapTablesSerializer('rows', $categories));
$categories = Fractal::create()
->collection($categories, new BootstrapTablesSerializer('rows', $categories->toArray() ))
->transformWith(CategoriesTransformer::class)
->serializeWith(new BootstrapTablesSerializer('rows', $categories));
//->toArray();
return $categories;
//$transformer = $categories->first()->transformer;
$resource = new Collection($categories->toArray(), new CategoriesTransformer(), 'rows');
//return $categories->transformWith(new CategoriesTransformer())->toArray();
//return new Collection($categories->toArray(), new CategoriesTransformer());
//return $this->transformData($categories, $transformer);
return $resource;
}

View file

@ -2,27 +2,72 @@
namespace App\Http\Serializers;
use League\Fractal\Pagination\CursorInterface;
use League\Fractal\Serializer\SerializerAbstract;
use League\Fractal\Resource\ResourceInterface;
use League\Fractal\Pagination\PaginatorInterface;
use League\Fractal\Serializer\ArraySerializer;
use League\Fractal\Manager;
use League\Fractal\Resource\Item;
use League\Fractal\Resource\Collection;
use League\Fractal\Serializer\JsonApiSerializer;
class BootstrapTablesSerializer extends ArraySerializer
class BootstrapTablesSerializer extends JsonApiSerializer
{
public function collection($resourceKey = null, array $data)
public function collection($resourceKey, array $data): array
{
\Log::error('resource key: '.$resourceKey);
return [$resourceKey => $data];
}
public function item($resourceKey, array $data): array
{
if ($resourceKey) {
return [$resourceKey => $data];
}
return $data;
}
public function item($resourceKey = null, array $data)
public function null(): ?array
{
return [];
}
public function includedData(ResourceInterface $resource, array $data): array
{
if ($resourceKey) {
return [$resourceKey => $data];
}
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.
}
}