channaveer's avatar

Laravel Pagination Not Working

Hi everyone, Laravel pagination is not working in one of my controller. In other controllers, it's working totally fine.

Following is the dump of my controller code

try {
            $courseCategory = (new CourseCategoryService)->getByColumnName('slug', 'course');

            $courseChannels        = Channel::with('course')
                ->where('course_category_id', $courseCategory->id)
                ->paginate(10);

            $this->pr($courseChannels->toArray());

            return view('user.course.free-course', [
                'courseCategory'    => $courseCategory,
                'courseChannels'    => $courseChannels
            ]);
        } catch (Exception $e) {
            session()->flash('error', $e->getMessage());
            return redirect()->route('auth.login');
        }

When I use $this->pr() to dump the code it shows the following which has links set perfectly fine

Array
(
    [current_page] => 1
    [data] => Array
        (
            [0] => Array
                (
                    [id] => 2462e6b5-f2e8-4012-b2cf-c8e7433b8edd
                    [course_category_id] => 8f6de4c0-833f-4012-a5da-e839aad23c1c
                    [name] => Patricia Douglas
                    [description] => Iure nihil tempore
                    [slug] => patricia-douglas
                    [icon] => 
                    [badge] => 
                    [sort_order] => 
                    [created_at] => 2021-06-15T07:34:08.000000Z
                    [updated_at] => 2021-06-15T07:34:08.000000Z
                    [deleted_at] => 
                    [course] => Array
                        (
                            [id] => 55690fc7-bc3e-4638-8c53-812a640ab3a9
                            [channel_id] => 2462e6b5-f2e8-4012-b2cf-c8e7433b8edd
                            [user_id] => df139348-e742-4650-941f-d07ccec03235
                            [language_id] => 61417d90-2d27-4f59-adac-0187a362416a
                            [course_type_id] => c9a2d704-4735-4514-a1ce-62c1684cdbb1
                            [currency_id] => 5baea2d4-d755-437e-add5-2ebb39597d89
                            [price] => 10000.00
                            [title] => 
                            [summary] => 
Some summary


                            [course_notes] => Voluptatum soluta su
                            [course_banner] => 
                            [created_at] => 2021-06-15T07:34:08.000000Z
                            [updated_at] => 2021-06-15T07:34:08.000000Z
                            [deleted_at] => 
                        )

                )

            [1] => Array
                (
                    [id] => 777818e6-8a5e-4d7e-b9e8-81e4502d3059
                    [course_category_id] => 8f6de4c0-833f-4012-a5da-e839aad23c1c
                    [name] => Azalia Bullock
                    [description] => Voluptas minima inci
                    [slug] => azalia-bullock
                    [icon] => 
                    [badge] => 
                    [sort_order] => 
                    [created_at] => 2021-06-01T10:15:46.000000Z
                    [updated_at] => 2021-06-10T10:15:46.000000Z
                    [deleted_at] => 
                    [course] => Array
                        (
                            [id] => 250643bf-c127-4995-a673-f6623cefd388
                            [channel_id] => 777818e6-8a5e-4d7e-b9e8-81e4502d3059
                            [user_id] => df139348-e742-4650-941f-d07ccec03235
                            [language_id] => 61417d90-2d27-4f59-adac-0187a362416a
                            [course_type_id] => c9a2d704-4735-4514-a1ce-62c1684cdbb1
                            [currency_id] => 5baea2d4-d755-437e-add5-2ebb39597d89
                            [price] => 10000.00
                            [title] => 
                            [summary] => 
Some summary


                            [course_notes] => Beatae molestiae eos
                            [course_banner] => 
                            [created_at] => 2021-06-10T10:15:46.000000Z
                            [updated_at] => 2021-06-10T10:15:46.000000Z
                            [deleted_at] => 
                        )

                )

        )

    [first_page_url] => http://localhost:8000/courses?page=1
    [from] => 1
    [last_page] => 1
    [last_page_url] => http://localhost:8000/courses?page=1
    [links] => Array
        (
            [0] => Array
                (
                    [url] => 
                    [label] => « Previous
                    [active] => 
                )

            [1] => Array
                (
                    [url] => http://localhost:8000/courses?page=1
                    [label] => 1
                    [active] => 1
                )

            [2] => Array
                (
                    [url] => 
                    [label] => Next »
                    [active] => 
                )

        )

    [next_page_url] => 
    [path] => http://localhost:8000/courses
    [per_page] => 10
    [prev_page_url] => 
    [to] => 2
    [total] => 2
)

My view page throws an exception. The following is code on the view page

@forelse ($courseChannels as $channel)
            <div class="post bg-white shadow-sm mb-5">
                @include('user.course._course')
            </div>
            @empty
            <div class="row">
                <div class="col-md-12">
                    <div class="post bg-white shadow-sm p-2 text-center">
                        <h5>Oops! No courses yet.</h5>
                    </div>
                </div>
            </div>
            @endforelse

            <div class="row">
                <div class="col-md-12 text-center">
                    {{ $courseChannels->links() }}
                </div>
            </div>

The following is the exceptions that are getting thrown

BadMethodCallException
Method Illuminate\Database\Eloquent\Collection::links does not exist. (View: /Applications/MAMP/htdocs/myproject/resources/views/user/course/free-course.blade.php)

Since I am using Bootstrap 4 I have already added the following code in AppServiceProvider

public function boot()
    {
        Paginator::useBootstrap();
}
0 likes
2 replies
Snapey's avatar
Snapey
Best Answer
Level 122

any chance you are overwriting $courseChannels somewhere else in the view?

Any view composers registered?

1 like
channaveer's avatar

Ah! With your suggestion, I saw in my AppServiceProviders. Dam! I had a little culprit variable name with that, which I used to send for all pages.

Thank you very much. It works amazing now.

Please or to participate in this conversation.