1

I'm trying to get the infinite loading / lazy loading working and below is my code.

Controller

$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();

View

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

Error

[2019-05-08 16:41:37] production.ERROR: Method Illuminate\Database\Eloquent\Collection::render does not exist.

1myb
  • 3,536
  • 12
  • 53
  • 73
  • I don't think putting presentation logic in the `with` would work. Maybe you need to rethink this – apokryfos May 08 '19 at 08:59
  • @apokryfos yea... i couldn't find solution for this, and the term "with" is tricky – 1myb May 08 '19 at 09:00
  • Instead of using the relationship you could create a custom query with the Query Builder for this to work. – Dan May 08 '19 at 09:02
  • @Dan could you please give me some example for this? Appreciate it!! Thanks – 1myb May 08 '19 at 09:03
  • If i need to load all the categories, and having the pagination for each categories. I'm out of idea – 1myb May 08 '19 at 09:05
  • Paginate the products for each category in a separate controller method e.g. `categories/{category}/products`. Start the page without any products and load the first page asynchronously. Not sure how to setup jquery-scroll to work with this though – apokryfos May 08 '19 at 09:23
  • try to dd the $categories variable and it need to give length aware paginator instance – ManojKiran A May 08 '19 at 09:24
  • Now that I'm thinking about this, I don't see a good use-case for jScroll here. To my understanding, it's supposed to get used once on the page and load new elements once you're close to the bottom of the page. – Dan May 08 '19 at 09:26
  • @Dan yea, jscroll doesn't affecting the render, it's actually working based on the Laravael pagination – 1myb May 08 '19 at 09:49
  • @Manojkiran.A Please check updated question, supposed to have a paginator variable? – 1myb May 08 '19 at 09:49
  • @apokryfos Thanks for the idea, if this is not working then will consider for altenative as suggested. – 1myb May 08 '19 at 09:50

1 Answers1

1

Because the method really does not exists :)

$categories is an object of Illuminate\Pagination\LengthAwarePaginator, because of your ->paginate(10) at the end of the chain:

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

$category->products is an object of Illuminate\Database\Eloquent\Collection.

You have to paginate products before calling render().

Try $category->products()->paginate(10)->render() inside blade view or

$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

Tarasovych
  • 2,228
  • 3
  • 19
  • 51
  • I've tried all your edit and no luck :'( [2019-05-08 17:36:31] production.ERROR: Method Illuminate\Database\Eloquent\Collection::paginate does not exist. (View: – 1myb May 08 '19 at 09:37
  • Can you `dd($category->products)`? What's in the output? @1myb – Tarasovych May 08 '19 at 09:38
  • You goal is to make `$category->products` paginated before you call `render()` – Tarasovych May 08 '19 at 09:38
  • Consider to have enough `product` to paginate https://github.com/laravel/framework/issues/19421 – Tarasovych May 08 '19 at 09:43
  • Please check the updated question. I have 200 dummy products in the db. – 1myb May 08 '19 at 09:50
  • @1myb have you tried `$category->products()->paginate(10)->render()`? – Tarasovych May 08 '19 at 09:51
  • Yep, and this is the error: 2019-05-08 17:52:55] production.ERROR: Method Illuminate\Database\Eloquent\Collection::paginate does not exist. (View: – 1myb May 08 '19 at 09:53
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/193021/discussion-between-1myb-and-tarasovych). – 1myb May 08 '19 at 09:53