Reputation: 2505
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
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');
Upvotes: 2
Reputation: 129
Here is your solution. Just change your return view option
return view('product')->with('products', $products);
Hope this helps you.
Upvotes: 1
Reputation: 396
In your Controller:
return view('product',compact('products');
In your View:
@foreach($products as $product)
{{ $product->name }}
@endforach
{{ $products->links() }}
Upvotes: 0
Reputation: 2474
Change to :
return view('product',compact('products'));
In View page try :
{{$products->render()}}
Upvotes: 0