TASKS 06 : Advanced CRUD Features & Data Validation - RadLeoOFC/laravel-admin-panel GitHub Wiki

Task Report: Advanced CRUD Features & Data Validation


Objectives

  • Add pagination to the product listing page.
  • Implement search and filtering by product name.
  • Introduce advanced validation for product creation and editing.
  • Enhance UI forms to display validation errors.
  • Commit changes to GitHub.

1. Adding Pagination

Steps:

  1. Opened ProductController.php and updated the index() method to include pagination.

  2. Updated index.blade.php to display pagination links using {{ $products->links() }}.

Updated index() method

use Illuminate\Http\Request; // Import Request

public function index(Request $request)
{
    $search = $request->input('search');

    $products = Product::with('category')
        ->where('name', 'LIKE', "%$search%")
        ->paginate(10); // Paginate 10 items per page

    return view('products.index', compact('products'));
}

Updated index.blade.php

<div class="mt-3 d-flex justify-content-center">
    {{ $products->links('pagination::bootstrap-5') }}
</div>

Screenshot after implementing pagination

Product table with plagination buttons



2. Implementing Search

Steps:

  1. Added a search bar in index.blade.php above the product table.

  2. Updated the index() method to filter products based on the search parameter.

Search bar in index.blade.php

<form method="GET" action="{{ route('products.index') }}" class="mb-3">
    <div class="input-group">
        <input type="text" name="search" class="form-control" placeholder="Search by name..." value="{{ request('search') }}">
        <button type="submit" class="btn btn-secondary">Search</button>
    </div>
</form>

Screenshot of search bar and results

Product table search bar

Result by searching with search bar



3. Improving Validation

Steps

  1. Updated store() and update() methods to enforce unique product names and price limits.

  2. Used firstOrCreate() to prevent duplicate categories when a new one is added.

Updated store() method

public function store(Request $request)
{
    $validated = $request->validate([
        'name' => 'required|string|max:255|unique:products,name',
        'price' => 'required|numeric|min:0|max:999999.99',
        'description' => 'nullable|string',
        'category_id' => 'nullable|exists:categories,id',
        'new_category' => 'nullable|string|max:255',
    ]);

    if ($request->filled('new_category')) {
        $category = Category::firstOrCreate(['name' => $request->new_category]);
        $validated['category_id'] = $category->id;
    }

    Product::create($validated);
    return redirect()->route('products.index')->with('success', 'Product created successfully.');
}

Screenshot of validation error on product creation

Error on product creation



4. Displaying Validation Errors

Steps:

  1. Updated create.blade.php and edit.blade.php to display validation errors in Bootstrap alert style.

Updated validation error block in create.blade.php & edit.blade.php

@if ($errors->any())
    <div class="alert alert-danger">
        <strong>Error!</strong> Please fix the following issues:
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

Screenshot of error message display

Product create with bootstrap styles

Product create error: enter the same product with bootstrap styles



5. Committing and Pushing Changes to GitHub

Steps:

  1. Added modified files to Git
  2. Committed with a meaningful message
  3. Pushed changes to GitHub

git add .

git commit -m "Added pagination, search, and advanced validation for products"

git push origin main

Screenshot of terminal with git commit and git push

Successful push on GitHab



Final Results

  1. Pagination added with 10 items per page.
  2. Search functionality implemented for filtering products.
  3. Advanced validation applied with unique names and price limits.
  4. Error messages displayed properly in forms.
  5. Changes committed and pushed to GitHub.

CRUD operations are now fully functional with pagination, search, and validation.

⚠️ **GitHub.com Fallback** ⚠️