Home > Net >  How do I do pagination on my code in Laravel?
How do I do pagination on my code in Laravel?

Time:10-27

So my front-end Lists of Businesses are not in paginated style. But I do not know how to do it. Can anyone please help? The code I posted is in my BusinessListController.php

BusinessListController.php

`<?php

namespace App\Http\Controllers;

use App\Models\Business;
use App\Models\Category;
use App\Models\Location;
use Illuminate\Http\Request;

class BusinessListController extends Controller
{
    public function index(Request $request)
    {
        $businesses = Business::query()
            ->with('location')
            ->whereFilters($request->only(
                ['search', 'category', 'location']
            ))
           ->get();d

        return view('pages.business-list', [
            'businesses' => $businesses,
            'locations' => Location::all(),
            'categories' => Category::all()
        ]);
    }
}`

And then here is the code for my view blade front-end Business-List.blade.php

<div >
            @foreach ($businesses as $business)
                <div >
                    <div >
                        <img
                            src="https://cdn1.clickthecity.com/images/articles/content/5d6eba1f4795e0.58378778.jpg"
                             alt="...">
                        <div >
                            <div >
                                <div>
                                    <h4  style="font-weight: bold;">
                                        {{Str::limit($business->name, 20, $end='...')}}
                                    </h4>
                                    <div >     
                                        <p >
                                            {{ $business->location?->name }}
                                        </p>                                                                             
                                        <p  style="color: #32a852;">
                                            {{ $business->category?->name}}
                                        </p>                                     
                                    </div>
                                </div>
                                <div >
                                    <a href="{{ route('store', $business->id) }}" >
                                        Visit
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            @endforeach
        </div>

CodePudding user response:

So you need to do three things.

  1. In Controller:

    $businesses = Business::query()
         ->with('location')
         ->whereFilters($request->only(
             ['search', 'category', 'location']
         ))
        ->paginate(15);
    

put the number of items you need on a single page. here I put 15.

  1. Put this under the </div> of your list.

    {{ $business->links() }}
    
  2. Put this inside the App\Providers\AppServiceProvider boot method.

     use Illuminate\Pagination\Paginator;
    
     public function boot()
     {
       Paginator::useBootstrapFive(); // or
       Paginator::useBootstrapFour();
     }
    

This depends upon which version of Bootstrap you are using.

Still confused? Checkout Laravel Pagination Documentation

CodePudding user response:

Just remove ->get();d and add paginate

example

ModelName()->paginate();
  • Related