CookieMonster's avatar

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

I implemented laravel scout(aglolia) for search indexing for my posts.

So in my index I have something like below:

 <div class="col-md-8">
            @forelse($threads as $thread)
            <div class="card mb-3">
                <div class="card-header">
                    <div class="level">
                        <h4 class="flex">   
                        <img src="{{asset('/images/uploads/threads/images/'.($thread->image ?? 'default-post-img.png'))}}"  height="60" width="60" alt="image">     
                         <a href="{{$thread->path()}}" style="text-decoration: none;">{{$thread->title}}</a>
                        </h4>
                        <a href="{{$thread->path()}}">{{$thread->replies_count}} {{Str::plural('reply',$thread->replies_count)}}</a>
                    </div>
                </div>
            </div>
            @empty
                <p>There are no threads at the moment.</p>
            @endforelse
            {{ $threads->links() }}

Now, the search function works normally but I added pagination to my threads and tried using the search function but it doesn't work and says:

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

how do I fix this if I want to use paginate?

0 likes
10 replies
neilstee's avatar

@nickywan123 your $threads doesn't look like a paginated result but a Collection instance. Can you show how you create $threads?

neilstee's avatar

@nickywan123 make sure paginate is correctly called like:

$threads = Thread::search('some text')->paginate(10);

and NOT:

$threads = Thread::search('some text')->get(); // result will be Collection
CookieMonster's avatar

This is my thread controller:

 public function index(Channel $channel, ThreadFilters $filters)
    {

        $threads = Thread::latest()->filter($filters);

        if($channel->exists){
            $threads->where('channel_id',$channel->id);
        }

        $threads = $threads->paginate(10);
        
        return view('threads.index',compact('threads'));
    }
CookieMonster's avatar

It shows:

#items: Illuminate\Database\Eloquent\Collection {#1479 ▼
    #items: array:10 [▼
      0 => App\Thread {#1467 ▶}
      1 => App\Thread {#1466 ▶}
      2 => App\Thread {#1465 ▶}
      3 => App\Thread {#1464 ▶}
      4 => App\Thread {#1463 ▶}
      5 => App\Thread {#1462 ▶}
      6 => App\Thread {#1461 ▶}
      7 => App\Thread {#1460 ▶}
      8 => App\Thread {#1459 ▶}
      9 => App\Thread {#1458 ▶}
    ]
  }

I mean the issue only happens when I try to use the search function.

neilstee's avatar

@nickywan123 as you can see, it becomes a Collection class instead of Pagination.

Is the index method the one that handles the search as well?

CookieMonster's avatar

I think it shows paginator:

Illuminate\Pagination\LengthAwarePaginator {#1492 ▼
  #total: 26
  #lastPage: 3
  #items: Illuminate\Database\Eloquent\Collection {#1479 ▶}
  #perPage: 10
  #currentPage: 1
  #path: "http://forum.test/threads"
  #query: []
  #fragment: null
  #pageName: "page"
  +onEachSide: 3
  #options: array:2 [▶]
}

I have a separate controller to handle the searching:

class SearchController extends Controller
{
    public function search(Request $request){
       
        $threads = Thread::search($request->input('search'))->get();
       

        return view('threads.index',compact('threads'));

    }
}

neilstee's avatar
neilstee
Best Answer
Level 34

@nickywan123 It should be:

class SearchController extends Controller
{
    public function search(Request $request){
       
        $threads = Thread::search($request->input('search'))->paginate(10);
       

        return view('threads.index',compact('threads'));

    }
}

I already told you to change ->get() to ->paginate() on my 2nd reply.

CookieMonster's avatar

My bad, I didn't see your edited 2nd reply. How come if I paginate my index() threads 10 per page, it means I have to paginate 10 for search() as well?

Please or to participate in this conversation.