【问题标题】:Laravel Pagination render method does not existLaravel 分页渲染方法不存在
【发布时间】:2019-05-08 08:38:55
【问题描述】:

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

控制器

$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] production.ERROR: Method Illuminate\Database\Eloquent\Collection::render 不存在。

【问题讨论】:

  • 我认为将表示逻辑放在with 中是行不通的。也许你需要重新考虑这个
  • @apokryfos 是的...我找不到解决方案,“with”这个词很棘手
  • 您可以使用查询生成器创建自定义查询,而不是使用关系。
  • @Dan 你能给我举个例子吗?欣赏它!谢谢
  • 如果我需要加载所有类别,并为每个类别进行分页。我没脑子

标签: php laravel laravel-5 jquery-jscroll


【解决方案1】:

因为方法真的does not exists:)

$categoriesIlluminate\Pagination\LengthAwarePaginator 的对象,因为你的-&gt;paginate(10) 在链的末尾:

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

$category-&gt;productsIlluminate\Database\Eloquent\Collection 的对象。

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

在刀片视图内尝试$category-&gt;products()-&gt;paginate(10)-&gt;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

【讨论】:

  • 我已经尝试了你所有的编辑,但没有运气:'( [2019-05-08 17:36:31] production.ERROR: Method Illuminate\Database\Eloquent\Collection::paginate 没有存在。(视图:
  • 你能dd($category-&gt;products)吗?输出中有什么? @1myb
  • 你的目标是在调用render()之前让$category-&gt;products分页
  • 考虑有足够的product分页github.com/laravel/framework/issues/19421
  • 请查看更新后的问题。我在数据库中有 200 个虚拟产品。
猜你喜欢
  • 2021-12-08
  • 1970-01-01
  • 1970-01-01
  • 2015-06-26
  • 1970-01-01
  • 2018-06-17
  • 2016-08-22
  • 2016-07-14
  • 2018-04-04
相关资源
最近更新 更多