Laravel pagination is a feature that allows you to break up large result sets into smaller, more manageable chunks. This is particularly useful when you have a large dataset to display to users, such as a list of blog posts, search results, or user records. Pagination divides the data into pages, and users can navigate through these pages using links.
Here's a step-by-step guide on how to use Laravel pagination:
Step 1: Configure Pagination in Your Controller In your controller method, where you retrieve data to be paginated, you'll use the paginate() method provided by Laravel's Eloquent ORM. This method automatically paginates the result set.
// Controller method example
use App\Models\Job; // Import your Job model
public function my_Jobs()
{
$id = Auth::user()->id;
$user_jobs = job::where('user_id',$id)->with('jobtype')->paginate(5); // Paginate the jobs, 5 items per page
return view('font.account.job.myJob',[ 'user_jobs' => $user_jobs, ]);
}
Step 2: Display Paginated Data in Your Blade View In your Blade view file, you can iterate over the paginated data using a loop, typically a foreach loop. Laravel's pagination system also provides a convenient method, links(), to display pagination links.
<!-- Blade view example (blade.php) -->
@foreach ($user_jobs as $user_job)
<p>{{ $user_job->title }}</p>
<!-- Display other job details -->
@endforeach
<!-- Display pagination links -->
<div>
{{ $user_jobs->links() }}
</div>
Notes: After complete above code implement then page refresh you display pagination numbering but not looking beautiful . if you display beautiful pagination in your laravel blade file , you can use css bootstrap class . here implement bootstrap two way :
<div>
{{ $user_jobs->links('pagination::bootstrap-5') }}
</div>
You write normal pagination code in your blade file, then go to App→providers → AppServiceProvider.php file.
import :
use Illuminate\Pagination\Paginator;
Then go to boot() function then write paginator::useBoostrapFive()
public function boot()
{
Paginator::useBootstrapFive();
}
<!-- Display pagination links -->
<div>
{{ $user_jobs->links() }}
</div>
Additional Configuration (Optional) : You can further customize pagination by passing additional options to the paginate() method, such as the number of items per page or customizing the query parameters.
// my jobs
public function my_Jobs()
{
$id = Auth::user()->id;
$perPage = 2; // Number of items per page
$columns = ['*']; // Retrieve all columns
$pageName = 'ocec'; // Name of the query string parameter for pagination
$pageNumber = 0; // Initial page number (optional)
$user_jobs = job::where('user_id',$id)->with('jobtype')->paginate($perPage, $columns, $pageName, $pageNumber);
return view('font.account.job.myJob',[ 'user_jobs' => $user_jobs, ]);
}
I'm a dedicated full-stack developer, entrepreneur, and the proud owner of ocec.org.in , hailing from the vibrant country of India. My passion lies in creating informative tutorials and sharing valuable tips that empower fellow artisans in their journey. With a deep-rooted love for technology, I've been an ardent enthusiast of PHP, Laravel, Angular, Vue, Node, JavaScript, jQuery, Codeigniter, and Bootstrap from their earliest days. My philosophy revolves around the values of hard work and unwavering consistency, driving me to continuously explore, create, and share my knowledge with the tech community.
helllo