Laravel分页渲染方法不存在

时间:2019-05-08 08:38:56

标签: php laravel laravel-5 jquery-jscroll

我正在尝试使无限加载/延迟加载正常工作,下面是我的代码。

控制器

$categories = Category::with(['products' => function ($query) {
                    $query->where('status', StatusConstant::PT_ACTIVE)->paginate(20);
                    $query->with(['purchased' => function ($query) {
                        $query->where('user_id', $this->user->id);
                    }])->paginate(20);
                }])->get();

查看

@foreach($categories as $category)
    <div id="tabs{{ $category->id }}" class="col s12">
        <div class="contents-tabs">
            @if (isset($category->products))
                <div class="infinite-scroll">
                    @forelse($category->products as $record)
                    <div class="cart-product first">
                        <div class="row">
                            <div class="col s4">
                                <div class="contents">
                                    <img src='{{ asset("/storage/uploads/$record->cover") }}' alt="">
                                </div>
                            </div>
                        </div>
                    </div>
                    @empty
                    <div class="cart-product first">
                        <div class="row">
                            <p class="promo">Null</p>
                        </div>
                    </div>
                    @endforelse
                    {!! $category->products->render() !!}
                </div>
            @endif
        </div>
   </div>
@endforeach

Js

$(function() {
        $('.infinite-scroll').jscroll({
            autoTrigger: true,
            loadingHtml: '<img class="center-block" src="{{ asset('images/loading.gif') }}" alt="Loading..." />',
            padding: 0,
            nextSelector: '.pagination li.active + li a',
            contentSelector: 'div.infinite-scroll',
            callback: function() {
                $('ul.pagination').remove();
            }
        });
    });

错误

  

[2019-05-08 16:41:37]生产。错误:方法Illuminate \ Database \ Eloquent \ Collection :: render不存在。

1 个答案:

答案 0 :(得分:1)

因为该方法实际上是does not exists:)

$categoriesIlluminate\Pagination\LengthAwarePaginator的对象,因为您的->paginate(10)位于链的末尾:

$query->with(['purchased' => function ($query) {
    $query->where('user_id', $this->user->id);
}])->paginate(10);
   //^ here

$category->productsIlluminate\Database\Eloquent\Collection的对象。

在致电products之前,您必须对render()进行分页。

在刀片视图中尝试$category->products()->paginate(10)->render()

$categories = Category::with(['products' => function ($query) {
                    $query->where('status', StatusConstant::PT_ACTIVE)->paginate(10);
                                                                      //^
                    $query->with(['purchased' => function ($query) {
                        $query->where('user_id', $this->user->id);
                    }])->paginate(10);
                }])->get();

Source