This may be more work than we want to do here tbh

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2024-07-18 14:14:40 +01:00
parent 52344c5574
commit 276950072f
3 changed files with 31 additions and 34 deletions

View file

@ -34,9 +34,9 @@ class CategoriesController extends Controller
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v4.0]
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
public function index(Request $request) : array
{
$this->authorize('view', Category::class);
$allowed_columns = [
@ -106,41 +106,21 @@ class CategoriesController extends Controller
$categories->where('checkin_email', '=', $request->input('checkin_email'));
}
$order = $request->input('order') === 'asc' ? 'asc' : 'desc';
$sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'assets_count';
$categories->orderBy($sort, $order);
$paginator = $categories->paginate(app('page_number'));
$total_results = $paginator->total();
$results = $paginator->getCollection();
$paginator = Category::paginate(2);
$categories = $paginator->getCollection();
$results = Fractal::create()
->collection($categories, new CategoriesTransformer())
return Fractal::create()
->collection($results, new CategoriesTransformer())
->serializeWith(new BootstrapTablesSerializer())
->addMeta(['total' => $total_results])
->paginateWith(new IlluminatePaginatorAdapter($paginator))
->toArray();
return $results;
$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

@ -13,10 +13,9 @@ class BootstrapTablesSerializer extends JsonApiSerializer
{
public function collection($resourceKey, array $data): array
{
\Log::error('resource key: '.$resourceKey);
return [
'total' => count($data),
'rows' => $data
'total' => count($data),
'rows' => $data
];
}

View file

@ -48,6 +48,24 @@ class SettingsServiceProvider extends ServiceProvider
return $offset;
});
// Make sure the offset is actually set and is an integer
\App::singleton('page_number', function ($results) {
if (request('page_number')) {
return (int) request('page_number');
}
$offset = app('api_offset_value');
\Log::error('offset is: '.$offset);
$limit = app('api_limit_value');
\Log::error('limit is: '.$limit);
$page_number = (intval($offset / $limit) + 1);
\Log::error('page number is: '.$page_number);
return $page_number;
});
/**
* Set some common variables so that they're globally available.