How to Add Pagination with Union in Laravel 10 | websolutioncode.com
How to Add Pagination with Union in Laravel 10 | websolutioncode.com

How to Add Pagination with Union in Laravel?

Introduction:

Pagination is a crucial feature when working with large datasets in web applications. Laravel, a popular PHP framework, provides a convenient way to implement Pagination with Union in Laravel. In this tutorial, we’ll explore how to add pagination to a query that involves the use of the union method in Laravel.

Step 1: Set Up Your Laravel Project

If you haven’t already, install Laravel using Composer:

composer create-project --prefer-dist laravel/laravel pagination-example

Navigate to the project directory:

cd pagination-example

Step 2: Create a Database and Model

Set up a database and create a model to represent the data you’ll be working with. For this example, let’s assume we have a posts table. Run the following commands:

php artisan make:model Post -m

This will create a migration file for the posts table and a corresponding model.

Step 3: Define the Database Schema

Edit the migration file at database/migrations/yyyy_mm_dd_create_posts_table.php to define the schema for the posts table. Add columns like title and content.

public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        $table->id();
        $table->string('title');
        $table->text('content');
        $table->timestamps();
    });
}

Run the migration to create the table:

php artisan migrate

Step 4: Seed the Database (Optional)

You can create a seeder to populate the posts table with some sample data:

php artisan make:seeder PostsTableSeeder

Edit the seeder file at database/seeders/PostsTableSeeder.php to insert dummy data.

public function run()
{
    \App\Models\Post::factory(50)->create();
}

Run the seeder:

php artisan db:seed --class=PostsTableSeeder

Step 5: Create a Controller

Generate a controller to handle the Pagination with Union in Laravel logic:

php artisan make:controller PostController

Step 6: Implement Pagination with Union

In the PostController.php file, define a method that uses the union method for pagination:

use App\Models\Post;
use Illuminate\Support\Facades\DB;

public function paginatedUnion()
{
    $firstQuery = Post::where('title', 'like', 'A%');
    $secondQuery = Post::where('title', 'like', 'B%');

    $result = $firstQuery->union($secondQuery)->paginate(10);

    return view('posts.index', ['posts' => $result]);
}

This example combines two queries using the union method and paginates the results.

Step 7: Create a Blade View

Create a Blade view file at resources/views/posts/index.blade.php to display the paginated data:

@foreach($posts as $post)
    <div>
        <h2>{{ $post->title }}</h2>
        <p>{{ $post->content }}</p>
    </div>
@endforeach

{{ $posts->links() }}

This view iterates over the paginated data and provides links for easy navigation.

Step 8: Route Configuration

Finally, define a route in routes/web.php to link to the controller method:

use App\Http\Controllers\PostController;

Route::get('/paginated-union', [PostController::class, 'paginatedUnion']);

Now, you can access the paginated data by visiting /paginated-union in your browser.

Conclusion:

In this tutorial, we’ve covered the steps to add Pagination with Union in Laravel. This approach allows you to combine and paginate data from multiple queries efficiently. By following these steps, you can enhance the user experience when dealing with large datasets in your Laravel application.

Leave a Reply