如何分页laravel收集

发布于 2025-01-30 02:35:11 字数 1101 浏览 4 评论 0 原文

嗨,我正在尝试分页所有这些元素,但是无论我尝试过什么,所有这些都显示在一页上,它不会在更多页面上拆分。

public function referralTransactions($id){
        $user_id=$id;
        $refs=User::where('sponsor_id', '=', $user_id)->pluck('id');
        $alltxns=[];
        $i=0;
        //$m=0;
        if(count($refs)>0) {
            foreach ($refs as $key =>$referral) {

                $alltxn = $this->buyselltransactionlist($referral);
                //$paginate = $this->getpaginateforbuysell($referral, 5);
                //$m=$m+count($alltxn);
                $alltxns[$i]=$alltxn;

                $i++;

            }
        }
        dd($alltxns);
        return view('adminpartials.referral_transactions',
            ['user_id'=>$user_id,
               // 'lists'=>$lists
                'alltxns'=>$alltxns,
                //'referrals'=>$referrals,
                'refs'=>$refs,     
            ]
        );```

enter image description here

Hi, I am trying to paginate all these elements, but whatever I tried it shows all on one page, it doesnt split this on more pages.

public function referralTransactions($id){
        $user_id=$id;
        $refs=User::where('sponsor_id', '=', $user_id)->pluck('id');
        $alltxns=[];
        $i=0;
        //$m=0;
        if(count($refs)>0) {
            foreach ($refs as $key =>$referral) {

                $alltxn = $this->buyselltransactionlist($referral);
                //$paginate = $this->getpaginateforbuysell($referral, 5);
                //$m=$m+count($alltxn);
                $alltxns[$i]=$alltxn;

                $i++;

            }
        }
        dd($alltxns);
        return view('adminpartials.referral_transactions',
            ['user_id'=>$user_id,
               // 'lists'=>$lists
                'alltxns'=>$alltxns,
                //'referrals'=>$referrals,
                'refs'=>$refs,     
            ]
        );```

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

以歌曲疗慰 2025-02-06 02:35:11
public function paginate($items, $perPage = 15, $page = null, $options = [])
{
    $page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
    $items = $items instanceof Collection ? $items : Collection::make($items);
    return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
}

$items = $this->paginate($collection);
public function paginate($items, $perPage = 15, $page = null, $options = [])
{
    $page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
    $items = $items instanceof Collection ? $items : Collection::make($items);
    return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
}

$items = $this->paginate($collection);
擦肩而过的背影 2025-02-06 02:35:11

这可以通过使用 Collection 宏来实现。

<?php

namespace App\Providers;

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

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        /**
         * Paginate a standard Laravel Collection.
         *
         * @param int $perPage
         * @param int $total
         * @param int $page
         * @param string $pageName
         * @return array
         */
        Collection::macro('paginate', function($perPage, $total = null, $page = null, $pageName = 'page') {
            $page = $page ?: LengthAwarePaginator::resolveCurrentPage($pageName);

            return new LengthAwarePaginator(
                $this->forPage($page, $perPage),
                $total ?: $this->count(),
                $perPage,
                $page,
                [
                    'path' => LengthAwarePaginator::resolveCurrentPath(),
                    'pageName' => $pageName,
                ]
            );
        });
    }
}

然后,您像其他任何收集助手一样使用。

collect([...])->paginate(15);

参考:

This can be achieved by using a Collection macro.

<?php

namespace App\Providers;

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

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        /**
         * Paginate a standard Laravel Collection.
         *
         * @param int $perPage
         * @param int $total
         * @param int $page
         * @param string $pageName
         * @return array
         */
        Collection::macro('paginate', function($perPage, $total = null, $page = null, $pageName = 'page') {
            $page = $page ?: LengthAwarePaginator::resolveCurrentPage($pageName);

            return new LengthAwarePaginator(
                $this->forPage($page, $perPage),
                $total ?: $this->count(),
                $perPage,
                $page,
                [
                    'path' => LengthAwarePaginator::resolveCurrentPath(),
                    'pageName' => $pageName,
                ]
            );
        });
    }
}

Then you use like any other collection helper.

collect([...])->paginate(15);

Ref:
https://gist.github.com/simonhamp/549e8821946e2c40a617c85d2cf5af5e

并安 2025-02-06 02:35:11

在控制器中使用paginate(您想要的数字),
示例:

#controller

$posts=Post::latest()->paginate(number you want)
return view('index')->with('posts', $posts);

在View

#loop项目

{{ $posts->links() }}

in controller use paginate(number you want perPage),
example :

#Controller

$posts=Post::latest()->paginate(number you want)
return view('index')->with('posts', $posts);

in View
#loop items

{{ $posts->links() }}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文