Hello, Visitor
Product Listing with Pagination With Laravel Livewire

Product Listing with Pagination With Laravel Livewire

LARAVEL LIVEWIRE
Hello, developers! Today, we delve into the realm of building a robust product listing with pagination using Laravel Livewire. Pagination is crucial for managing large datasets and enhancing user experience. Before we dive in, be sure to explore our recent tutorials:

Step 1: Create Product Model and Migration

Begin by creating a Product model and migration to store product information in your database:

php artisan make:model Product -m
Schema::create('products', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->text('description');
    $table->decimal('price', 8, 2);
    $table->timestamps();
});

This step establishes the foundation for storing product details in your Laravel application.

Step 2: Implement Product Listing Component

Create a Livewire component to handle product listing with pagination. Define the necessary properties and methods within your Livewire component:

php artisan make:livewire ProductList
class ProductList extends Component
{
    public $perPage = 10;
    public $search = '';

    public function render()
    {
        $products = Product::where('name', 'like', '%'.$this->search.'%')->paginate($this->perPage);
        return view('livewire.product-list', ['products' => $products]);
    }
}

By utilizing Livewire, you can seamlessly fetch and display paginated product data.

Step 3: Create Product Listing Blade View

Generate the blade view to render the paginated product list:

<div>
    <input type="text" wire:model="search" placeholder="Search Products...">

    <ul>
        @foreach($products as $product)
            <li>{{ $product->name }} - ${{ $product->price }}</li>
        @endforeach
    </ul>

    {{ $products->links() }}
</div>

This view displays the paginated product data along with a search input for filtering products.

Step 4: Implement Search Functionality

Enhance your Livewire component with search functionality to filter products based on user input:

public function updatedSearch()
{
    $this->resetPage();
}
use Livewire\WithPagination;

class ProductList extends Component
{
    use WithPagination;

    public $perPage = 10;
    public $search = '';

    public function render()
    {
        $products = Product::where('name', 'like', '%'.$this->search.'%')->paginate($this->perPage);
        return view('livewire.product-list', ['products' => $products]);
    }
}

The search functionality allows users to dynamically filter products without reloading the page.

Step 5: Add Pagination Controls

Incorporate pagination controls to navigate through the paginated product list:

<div>
    {{ $products->links() }}
</div>

These pagination links enable seamless navigation between different pages of product listings.

Step 6: Customize Pagination Styling

Customize the pagination styling to match your application's design by leveraging Laravel Livewire's pagination customization:

<div>
    {{ $products->links('livewire.custom-pagination-links') }}
</div>

This approach allows you to tailor the pagination appearance to align with your application's aesthetics.

Step 7: Optimize Pagination Query

Optimize the pagination query by eager loading relationships and minimizing database queries:

$products = Product::with('category')->where('name', 'like', '%'.$this->search.'%')->paginate($this->perPage);
public function render()
{
    $products = Product::with('category')->where('name', 'like', '%'.$this->search.'%')->paginate($this->perPage);
    return view('livewire.product-list', ['products' => $products]);
}

Eager loading relationships enhances performance by fetching related data efficiently.

Step 8: Handle Empty Pagination Results

Address scenarios where the paginated product list returns no results by displaying a message to the user:

<div>
    @if($products->isEmpty())
        <p>No products found.</p>
    @else
        <ul>
            @foreach($products as $product)
                <li>{{ $product->name }} - ${{ $product->price }}</li>
            @endforeach
        </ul>
    
        {{ $products->links() }}
    @endif
</div>

Handling empty pagination results gracefully ensures a positive user experience.

Step 9: Add Pagination Meta Information

Include pagination meta information to provide users with contextual details about the current page and total items:

<div>
    <p>Displaying {{ $products->count() }} out of {{ $products->total() }} products.</p>
    {{ $products->links() }}
</div>

This meta information enhances user understanding of the paginated product list.

Step 10: Implement Pagination Refresh

Implement a pagination refresh mechanism to reload the paginated list upon specific actions or changes:

public function refreshProducts()
{
    $this->render();
}

Refreshing the pagination ensures that the displayed product list stays up to date with any modifications.

Step 11: Handle Pagination Edge Cases

Address edge cases such as navigating beyond the available pages or invalid page numbers by handling these scenarios gracefully:

public function updatedPage($value)
{
    if ($value <= 0 || $value > $this->products->lastPage()) {
        $this->resetPage();
    }
}

Properly managing pagination edge cases ensures a seamless user experience.

Step 12: Conclusion

Congratulations! You've built a dynamic product listing with pagination using Laravel Livewire. By following these steps, you've empowered your application with efficient data handling and improved user interaction. Keep exploring the possibilities of Laravel Livewire to create even more impactful web applications!

Like 0
Related Posts
Laravel Livewire CRUD with Bootstrap Modal
Laravel Livewire CRUD with Bootstrap Modal
29 December 2021 · 14.7K Views
Laravel Livewire Image Upload with Preview
Laravel Livewire Image Upload with Preview
01 January 2022 · 10.7K Views
Laravel Livewire Installation & Layout Setup
Laravel Livewire Installation & Layout Setup
05 February 2022 · 3.8K Views

Comments (0)

No comments yet. Be the first to leave your thoughts!

Leave a Comment

You need to login to post a comment

Login to Comment