User57
User57

Reputation: 2505

Laravel Paginate not working

I was trying to paginate some data with Query builder with the following query.

public function productSearch(Request $request)
{
     $name = $request->name; 
     $products = DB::table('products')                  
         ->where('name', 'LIKE', "%$name%")             
         ->paginate(4);

     return view('product',compact('products');
}

But when I tried to show the pagination in the view page with the following line

{{ $products->links() }}

It shows

Method links does not exist

What could the possible error be since the pagination does not show?

Upvotes: 0

Views: 20636

Answers (5)

Shubham Gupta
Shubham Gupta

Reputation: 331

2 types to print pagination link in laravel - Try with -

use Illuminate\Support\Facades\DB;
public function productSearch(Request $request)
{
     $name = $request->name; 
     $products = DB::table('products')                  
         ->where('name', 'LIKE', "%$name%")             
         ->paginate(4);

     return view('product',['products' => $products]);
}

In View -

<div class="container">
    @foreach($products as $product)
        <h4>{{ $product['name'] }}</h5> <br>
    @endforeach
</div>

1st type to defined laravel pagination link -

   {{ $products->links() }}

And 2nd type to defined laravel pagination link -

   {{ $products->render(); }}

Note - Also you missed in your code -> return view('product',compact('products')); in return view('product',compact('products'); Desired output -

Upvotes: 2

Prabal Thakur
Prabal Thakur

Reputation: 129

Here is your solution. Just change your return view option

return view('product')->with('products', $products);

Hope this helps you.

Upvotes: 1

bafdurango
bafdurango

Reputation: 40

try with:

{{ $products->render() }}

Upvotes: 0

Roland Allla
Roland Allla

Reputation: 396

In your Controller:

return view('product',compact('products');

In your View:

@foreach($products as $product)
  {{ $product->name }}
@endforach

{{ $products->links() }} 

Upvotes: 0

Rahul
Rahul

Reputation: 2474

Change to :

return view('product',compact('products'));

In View page try :

{{$products->render()}}

Upvotes: 0

Related Questions