Exporting Filtered Products from the Admin Grid
As a senior developer, I understand your need for efficient data management within the admin panel of your Laravel application. In this post, we’ll walk through how to add an “export” button to the manage products page and export the filtered collection as a CSV file.
Overriding the Manage Products Blade File
Firstly, you’ll want to override the admin/products/index.blade.php blade template located in your package’s resources directory. This is where the admin product grid lives.
// AdminProductsServiceProvider.php
public function boot(): void
{
$this->loadViewsFrom(__DIR__.'/../resources/views', 'admin_products');
}
You can then create a custom version of this template to include your export button:
<!-- resources/views/admin/products/index.blade.php -->
@extends('admin::layouts.app')
@section('content')
<div class="container">
<h1>Manage Products</h1>
<!-- Your existing filters here -->
@include('admin_products::filters')
<button id="export-button" class="btn btn-primary">Export</button>
<!-- Your existing products table -->
<table class="table">
<!-- Table headers and rows -->
</table>
</div>
<script src="{{ asset('js/export.js') }}"></script>
@endsection
Creating the Export JavaScript
We’ll write a short JavaScript file (resources/js/export.js) that handles the export button click event:
// resources/js/export.js
document.getElementById('export-button').addEventListener('click', function() {
var url = '{{ route('admin_products.export') }}';
window.location.href = url;
});
Creating the Export Route and Controller Method
Next, add a new export route to your AdminProductsServiceProvider:
// AdminProductsServiceProvider.php
public function registerRoutes(): void
{
Route::get('/admin/products/export', [ProductController::class, 'export'])->name('admin_products.export');
}
In your ProductController, create the export method:
// ProductController.php
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\ProductsExport;
public function export()
{
return Excel::download(new ProductsExport, 'products.csv');
}
Creating the Products Export Class
Finally, create a new class app/Exports/ProductsExport.php to handle the actual CSV export:
// app/Exports/ProductsExport.php
use Maatwebsite\Excel\Concerns\FromCollection;
use App\Models\Product;
class ProductsExport implements FromCollection
{
public function collection()
{
// Assuming you have a filtered collection in session
return session()->get('filtered_products');
}
}
Putting It All Together
Here’s how the flow works:
- The admin filters products on the manage products page.
- When the “export” button is clicked, it triggers the JavaScript event listener which redirects to the
/admin/products/exportroute. - The
exportmethod in your controller uses theProductsExportclass to generate a CSV file from the filtered product collection stored in the session.
Remember to install the necessary packages via composer:
composer require maatwebsite/excel
This guide should provide you with a solid foundation for adding the export functionality to your application. If you’d like more detailed examples or want to explore other data management features, feel free to visit our blog at Laravel Company.