I used many functions from class to be used in a controller and this is a normal way to prevent duplicate of the code, but I have a function that store distance in a model and use collection for pagination, this function works fine and return $stores variable in the controller to be used for pagination, now I need to put it in a class and call it from the controller, unfortunately it returns null value!!! how can I fix this issue? the same function in the controller works fine but if i put it in a class and called from controller will return null, please help me
Class:
public function getStoresDistance($allstores)
{
$stores = collect([]);
foreach (session('storeinfo') as $storeInfo) {
$store = $allstores->find($storeInfo['id']);
if ($store) {
$store->distance = $storeInfo['distance'];
$stores[] = $store;
if (!Collection::hasMacro('paginate')) {
Collection::macro('paginate', function ($perPage = 25, $page = null, $options = []) {
$options['path'] = $options['path'] ?? request()->path();
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
return new LengthAwarePaginator(
$this->forPage($page, $perPage)->values(),
$this->count(),
$perPage,
$page,
$options
);
});
}
}
}
}
call from controller:
$allstores = Storeinfo::where('show', 'y')->get();
$findstores = Helper::getStoresDistance($allstores);
