简体   繁体   中英

Laravel, how to sort and paginate of loop array

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

First, I would create a collection instance of your $stores array:

$stores = collect([]);

I prefer using the push() api of the collection to add items:

$stores->push($store);

Second, the collection instance doesn't provide a native way to paginate so you need to add a macro:

use Illuminate\Support\Collection;
use Illuminate\Pagination\Paginator; 
use Illuminate\Pagination\LengthAwarePaginator;

...

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
        );
    });
}

I personally added the above macro in AppServiceProvider so that I can use it anywhere in my project. This should be located in the app/Providers directory.

To paginate you simply need to call the paginate() macro you just created:

$stores->paginate(15);

If you wish to set the current page or path, you may do so:

$stores->paginate(15, 1, ['path' => 'your/custom/path']);

To sort, all you need to do is used the desired sortBy method to achieve your results.

Per the docs, sortBy takes a string:

$sorted = $collection->sortBy('price');

Or a callback:

$sorted = $collection->sortBy(function ($product, $key) {
    return count($product['colors']);
});

The method, sortByDesc() works the same way as sortBy() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM