简体   繁体   English

Laravel,如何对循环数组进行排序和分页

[英]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: 我使用以下方法将ID放入会话中:

 $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 $ mile来自距离的计算,请在此处输入代码

First, I would create a collection instance of your $stores array: 首先,我将创建您的$stores数组的集合实例:

$stores = collect([]);

I prefer using the push() api of the collection to add items: 我更喜欢使用集合的push() API添加项目:

$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. 我亲自在AppServiceProvider添加了上面的宏,以便可以在项目中的任何地方使用它。 This should be located in the app/Providers directory. 它应该位于app/Providers目录中。

To paginate you simply need to call the paginate() macro you just created: 要进行分页,只需要调用刚创建的paginate()宏即可:

$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. 要进行排序,只需使用所需的sortBy方法即可实现结果。

Per the docs, sortBy takes a string: 根据文档, sortBy采用一个字符串:

$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() . 该方法, sortByDesc()的工作方式相同sortBy()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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