I have this code in controller, so I need to paginate and sort by using distance, I dont know how to do this, Im new to laravel , thanks in advance
$stores = [];
foreach (session('storeinfo') as $storeInfo) {
$store = Storeinfo::find($storeInfo['id']);
if ($store) {
$store->distance = $storeInfo['distance'];
$stores[] = $store;
$stores = collect([]);
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
);
});
}
}
}
return view('stores.archive',compact('stores'));
Im placing id into session using this:
$allstores= Storeinfo::all();
foreach ($allstores as $allstore) {
Session::push('storeinfo', [
'id' => $allstore->id,
'distance' => $miles
]);
}}
where $mile comes from calculation of distance enter code here
Model::find(...is typically used for finding a single item(row) in a table. Pagination doesn't apply to a single item. Can you provide more context to your problem? How are you placing storeinfo items into session? Is$storeInfo['id']a foreign key? I'm assuming you have a Store to StoreInfo model relationship, if so, the code in these models will be useful here.