Support the ongoing development of Laravel.io →
Eloquent

I’ve got an admin-type page that allows the use to either

  1. Browse using pagination through all courses
  2. Search for a course

There are some basic limitations in this, as you cannot search for more than 50 courses. I don’t want to get into a pagination by query result issue here, as that requires screwing around with the pagination more than I’d like to.

What I’d like to do is simply work out in the view whether or not to use:

{{ $courses->links() }}

At the moment, if I make a search, it doesn’t use pagination, so of course I get:

Method Illuminate\Database\Eloquent\Collection::links does not exist

Here’s my method:

public function index(Request $request)
{
    $query = $request->query('q');

    $courses = Course::orderBy('name');

    if ($query) {
        $courses = $courses->where('name', $query)->limit(50);
    } else {
        $courses = $courses->paginate(50);
    }

    return view('courses.index')
        ->with('q', $query)
        ->with('courses', $courses);
}
Last updated 3 years ago.
0

Try this {{isset($courses->links()) ? $courses->links() : null }}

Last updated 5 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.