What is laravel pagination and how to use step by step guide by ocec

What is laravel pagination and how to use step by step guide by ocec

Laravel Pagination :

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

1st way : 

<div>
{{ $user_jobs->links('pagination::bootstrap-5') }}

</div>

 

2nd way : 

 You write  normal pagination code in your blade file,  then go to AppprovidersAppServiceProvider.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>
 

Notes :

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, ]);

    }
 


Share this article







Related Posts




0 Comments



Load more Comments

Post a Comment


helllo
Ocec Copyright text of dont't copyright our content