Syed1980's avatar

I'm getting this error "Method Illuminate\Database\Eloquent\Collection::links does not exist.".

Hello All,

I'm getting this error "Collection::links does not exist." while trying to perform a simple search with pagination in my Laravel application. I don't know what's wrong with my code, can anyone please check and advise?

	public function index(Request $request){
   		$products = Product::paginate(10);
   		 if($request->has('search')){
        	$products = Product::where('product_name', 'like', "%{$request->search}%")
					->orWhere('product_description', 'like', "%{$request->search}%")->get();
   		}
    		return view('products.index', compact('products'));
	}

Code from the views.

					<div class="col d-inline-flex">
                <form action="{{ route('products.index') }}" method="GET" class="col d-inline-flex">
                    <div class="form-row align-items-center col d-inline-flex">
                        <div class="col">
                            <input type="search" name="search" class="form-control mb-2" id="inlineFormInput" placeholder="Search by Product">
                        </div>

                        <div class="col">
                            <button style="margin-left: 1rem;" type="submit" class="btn btn-primary mb-2">Submit</button>
                        </div>
                    </div>
                </form>
                <div class="col-auto">
                    <a href="{{ route('products.index')}}" class="btn btn-primary mt-1 mb-2">Display All</a>
                    <a href="{{ route('products.create')}}" class="btn btn-primary mt-1 mb-2 mx-3">Create Product</a>
                </div>
                <div class="mt-1">
                    {!! $products->links() !!}
                </div>
            </div>
0 likes
5 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

You need to return a pagination instance in both cases

	public function index(Request $request){
   		$products = Product::paginate(10);
   		 if($request->has('search')){
        	$products = Product::where('product_name', 'like', "%{$request->search}%")
					->orWhere('product_description', 'like', "%{$request->search}%")->paginate(10);
   		}
    		return view('products.index', compact('products'));
	}
1 like
Syed1980's avatar

With your kind permission, can I ask a question? I have a doubt. If my search involves more than 1 table then how I can join tables in my query?

Sinnbeck's avatar

@Syed1980 You are free to ask but if it isnt related to the original question, you will probably have more luck creating a new thread :)

Syed1980's avatar

Ohh yeah! Sure I will create a new one. Thanks.

Please or to participate in this conversation.