kmj1993
136
2
Laravel

want to add pagination to search product page in laravel

Posted 1 year ago by kmj1993

I want to add pagination to my searching product item view using my show controller

This is my controller

public function show($id) { $categories = Item::all();

$products = Item::find($id)->products->paginate(3);

return view('category.index', compact('categories', 'products'));

} This is my view

@extends('layout.front')

@section('page')

<!-- Page Content -->
<div class="container">

    <div class="row">

        <div class="col-lg-3" style="margin-top:20px;">

            <nav class="main-nav">
                <ul class="main-nav-ul">
                    <li style="background-color: #343A40; color: #ffffff; font-weight: 600;" id="sidebar-header"><a>Product List</a></li>
                    <li><a href="{{ url('/computer') }}" style="font-weight: 600;">Computer<span></span></a>
                        <ul>
                            @if(!empty($categories))
                                @forelse($categories as $category)
                                    <li><a href="{{ route('category.show', $category->id)}}">{{ $category->name }}</a></li>

                                @empty
                                    <li>No data found</li>
                                @endforelse
                            @endif
                        </ul>
                    </li>
                    <li><a href="#" style="font-weight: 600;">CCTV</a></li>
                    <li><a href="#" style="font-weight: 600;">Gaming</a></li>
                </ul>
            </nav>

        </div>
        <!-- /.col-lg-3 -->

        <div class="col-lg-9">


            <div class="row" style="margin-top:20px;">


                @if(!empty($products))
                    @foreach($products as $key=>$product)
                        <div class="col-md-4 col-sm-6 portfolio-item" id="items">
                            <div class="card h-100">

                                <a href="#"><img class="card-img-top" src="/storage/{{ $product->image }}" alt="Product Image"></a>
                                <div class="card-body">
                                    <h4 class="card-title">
                                        <a href="#">{{ $product->category_name }}<br />{{ $product->item_name}}</a>
                                    </h4>
                                    <p class="card-text" style="color: #A9A9A9;text-decoration: line-through;">LKR {{ $product->old_price}}</p>
                                    <h4 style="text-align: center; color: #fff;"><a class="waves-effect waves-light btn btn-dark btn-block">LKR {{ $product->new_price}}</a></h4>

                                    {{[email protected]}}
                                </div>
                            </div>
                        </div>
                    @endforeach
                @endif

            </div>
            <!-- /.row -->

        </div>
        <!-- /.col-lg-9 -->

    </div>
    <!-- /.row -->

</div>
<!-- /.container -->
{!! $products->links() !!} @endsection

i used laravel default pagination and customize my bootstrap pagination to laravel.

But in my show function in controller pagination not working. how to fix searching products pagination.

i used links method. but it's not working for this view.

{!! $comproducts->links() !!}

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.