TASKS 06 : Advanced CRUD Features & Data Validation - RadLeoOFC/laravel-admin-panel GitHub Wiki
- 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.
-
Opened
ProductController.phpand updated theindex()method to include pagination. -
Updated
index.blade.phpto display pagination links using{{ $products->links() }}.
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'));
}<div class="mt-3 d-flex justify-content-center">
{{ $products->links('pagination::bootstrap-5') }}
</div>
-
Added a search bar in
index.blade.phpabove the product table. -
Updated the
index()method to filter products based on thesearchparameter.
<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>

-
Updated
store()andupdate()methods to enforce unique product names and price limits. -
Used
firstOrCreate()to prevent duplicate categories when a new one is added.
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.');
}
- Updated
create.blade.phpandedit.blade.phpto display validation errors in Bootstrap alert style.
@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

- Added modified files to Git
- Committed with a meaningful message
- Pushed changes to GitHub
git add .
git commit -m "Added pagination, search, and advanced validation for products"
git push origin main

- Pagination added with 10 items per page.
- Search functionality implemented for filtering products.
- Advanced validation applied with unique names and price limits.
- Error messages displayed properly in forms.
- Changes committed and pushed to GitHub.
CRUD operations are now fully functional with pagination, search, and validation.