I’m trying to create a custom pagination in laravel 10, but I’m getting the error code
Call to a member function links() on array
this is my code:
CulinaryController.php
public function index(Request $request)
{
$client = new Client();
$qs = http_build_query($request->query());
$qs = $qs ? '?'.$qs : '';
$url = initURLPath("culinary" . $qs);
$response = $client->get($url);
// dd($response->getBody()->getContents());
$json = json_decode((string)$response->getBody()->getContents(), true);
if ($request->query('dd')) {
// dd($response->getBody());
dd($json);
}
$view_data = [
'title' => 'Browse Culinary',
'data' => $json,
];
return view('culinary.index', $view_data);
}
Api/V1/CulinaryController.php
public function index(Request $request)
{
$perPage = $request->query('per_page', 8);
$currentPage = $request->query('page', 1);
$pageOffset = ($currentPage - 1) * $perPage;
$result = Culinary::get();
$data = $result->toArray();
$resultWithPagination = new LengthAwarePaginator(
array_slice($data, $pageOffset, $perPage),
count($data),
$perPage,
LengthAwarePaginator::resolveCurrentPage(),
[
'path' => LengthAwarePaginator::resolveCurrentPath(),
'query' => $request->query(),
]
);
return $resultWithPagination;
}
and this is the code that causes the error in the blade view:
{{ $data->links() }}
any help is appreciated.