1

I'm trying to set pagination in a Laravel blade/view but it's showing an error with this message below:

BadMethodCallException

Method Illuminate\Database\Eloquent\Collection::paginate does not exist.

Controller

public function view()
{
    $user = Auth::user();
    $basic_info = User::find($user->id)->basic_info;
    $category = Category::all()->paginate(10); 

    return view('admin.article.category-view')->with(['user' => $user, 'basic_info' => $basic_info, 'category' => $category]);
}

View Blade (admin.article.category-view)

<div class="panel-body">
    <table class="table table-hover">
        <thead>
        <tr>
            <th>Category Name</th>
        </tr>
        </thead>
        <tbody>
        @foreach($category as $cat)
            <tr>
                <td>{{ $cat->name }}</td>
            </tr>
        @endforeach
        </tbody>
    </table>
    {{ $category->links() }}
</div>
0

2 Answers 2

5

Using paginate method on the query builder or an Eloquent query only, not on collection, like so:

public function view()
{
    $user = Auth::user();
    $basic_info = User::find($user->id)->basic_info;
    $category = Category::paginate(10); 

    return view('admin.article.category-view')->with(['user' => $user, 'basic_info' => $basic_info, 'category' => $category]);
}
Sign up to request clarification or add additional context in comments.

Comments

1

You need to remove all():

 $category = Category::paginate(10); 

When you're using all() you get all the rows from the table and get a collection

You can only invoke "paginate" on a Query, not on a Collection.

1 Comment

It worked. Now I can understand how collections work. Thanks a lot for enlightening me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.