# Add a CSV Import to a Filament Resource

Yes. An agent writes a ProductImporter in the style of the existing category importer, adds an ImportAction to the products list, tests it with Pest and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-csv-import/filament/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads the importer the project already has before it writes another. In an app shaped like filamentphp/demo, `composer.json` requires `filament/filament` ^5.0 on `laravel/framework` ^12 with `pestphp/pest` ^3, `larastan/larastan` ^3.1, `laravel/pint` and `rector/rector` for development, and `CLAUDE.md` carries the Laravel Boost guidelines: follow sibling files, do not add dependencies, write tests instead of verification scripts. `app/Filament/Imports/Shop/CategoryImporter.php` extends `Importer` for `ProductCategory`: `ImportColumn` entries with `requiredMapping()`, `rules()` and `example()`, a `parent` column resolved through `relationship(resolveUsing: ['name', 'slug'])`, `numeric()` and `boolean()` casts, a `resolveRecord()` that does `firstOrNew` on `slug`, and a `getCompletedNotificationBody()` that counts successful and failed rows. `app/Filament/Resources/Shop/Categories/Pages/ListCategories.php` mounts it with `ImportAction::make()->importer(CategoryImporter::class)` next to `CreateAction`. `ListProducts.php` has only `CreateAction` and the header widgets. `app/Models/Shop/Product.php` has `brand()` as `belongsTo`, `productCategories()` as `belongsToMany` through `product_category_product`, boolean casts for `featured`, `is_visible`, `backorder` and `requires_shipping`, and a `date` cast on `published_at`; the `products` migration gives `slug`, `sku` and `barcode` unique nullable columns and an enum `type` of `deliverable` or `downloadable`. The `imports` and `failed_import_rows` tables already exist in `database/migrations`, and `.env.example` sets `QUEUE_CONNECTION=sync`.

`app/Filament/Imports/Shop/ProductImporter.php` follows the category importer column for column: `name` and `sku` with `requiredMapping()` and `required` rules, `slug` filled from the name when the file leaves it empty, `barcode`, `brand` through `relationship(resolveUsing: ['name', 'slug'])`, `description`, `price`, `old_price` and `cost` as `numeric()` with a two-decimal rule, `qty` and `security_stock` as integers, `is_visible`, `featured`, `backorder` and `requires_shipping` as `boolean()`, `type` with an `in:deliverable,downloadable` rule, and `published_at` cast to a date. `resolveRecord()` does `firstOrNew` on `sku`, so a second upload of the same file updates rather than duplicates. Categories come in as a comma-separated column of slugs: `ImportColumn::relationship()` resolves a `belongsTo`, so the agent syncs `productCategories()` in `afterSave()` without detaching ones the row does not mention. Every column carries an `example()` so the example CSV the modal offers imports cleanly. `ListProducts::getActions()` gains `ImportAction::make()->importer(ProductImporter::class)` before `CreateAction`, the same order as the categories page. With `QUEUE_CONNECTION=sync` the rows import inside the request; on a queue they run as jobs and the user is told through the panel's database notifications, which `AdminPanelProvider` already enables.

`tests/Filament/Resources/Shop/Products/Pages/ListProductsTest.php` tests the page with `Livewire::test(ListProducts::class)`, `TestAction` and factories, under the `RefreshDatabase` and `actingAs` setup in `tests/Pest.php`. The agent adds tests there: one uploads a two-row CSV built with `UploadedFile::fake()->createWithContent()` through the import action with its column map, then asserts both products exist with their brand and categories attached and that the `imports` row counts two successful rows; another sends a row with a non-numeric price and asserts it lands in `failed_import_rows` with a validation message while the good row still imports; a third uploads the same file twice and asserts there is still one product per `sku`. It runs `composer test:pest`, `vendor/bin/pint --test` and `composer test:phpstan` at larastan level 8, the checks the project configures, and `.github/workflows/tests.yml` runs the Pest suite on PHP 8.4 with SQLite.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add a CSV import for products to this Filament app and open a pull request. Read composer.json, CLAUDE.md, app/Filament/Imports/Shop/CategoryImporter.php, app/Filament/Resources/Shop/Categories/Pages/ListCategories.php, app/Filament/Resources/Shop/Products/Pages/ListProducts.php, app/Filament/Resources/Shop/Products/Schemas/ProductForm.php, app/Models/Shop/Product.php, app/Models/Shop/ProductCategory.php, the products migration, tests/Pest.php, tests/Filament/Resources/Shop/Products/Pages/ListProductsTest.php, phpstan.neon and .github/workflows/tests.yml first. Write app/Filament/Imports/Shop/ProductImporter.php in the shape of CategoryImporter with columns for name, slug, sku, barcode, brand (resolved by name or slug), description, price, old_price, cost, qty, security_stock, is_visible, featured, backorder, requires_shipping, type and published_at, each with rules and an example, resolveRecord by sku, categories from a comma-separated slug column synced in afterSave, and a completed-notification body. Add ImportAction::make()->importer(ProductImporter::class) before CreateAction in ListProducts. Add Pest tests in ListProductsTest.php covering a two-row import with brand and categories, a failed row recorded in failed_import_rows beside a good one, and a repeated upload that updates rather than duplicates. Run composer test:pest, vendor/bin/pint --test and composer test:phpstan and paste the output in the pull request. Do not add a dependency or change the product form.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request that adds `app/Filament/Imports/Shop/ProductImporter.php`, edits `ListProducts.php` and `ListProductsTest.php`, and leaves the product form and table alone. The description lists the columns with the example CSV header and pastes the `pest`, `pint` and `phpstan` output.

On a repository with the `tests` workflow, the pull request installs Composer and npm dependencies, builds the assets, creates the SQLite database and runs `composer test:pest`, so the three new tests run there with the rest of the suite.

A reviewer checks that `sku` is the right identity for their catalogue, that a brand name shared by two brands is refused rather than guessed, that the decimal rule matches the form's, that `afterSave` never detaches categories the file does not mention, and who may see the import action. Then they set the queue connection the production import should use.

## Run it on every resource that needs an import

Each resource that needs an import is one more run: `POST /o/{org}/sessions` naming the model and its columns, with the repository in `resources`. The agent writes the importer beside `ProductImporter`, mounts the action on that resource's list page and adds the tests, so every importer in the app has the same shape.

Give the API key a `webhook_url` and Rush POSTs `run.completed` when each turn ends, with the `session_url` to read the pull request and the test output from.

## Why run it on Rush?

An import is a data path that runs on files other people make, and its failure modes, a bad decimal, an unknown brand, a repeated upload, only show with a real database and a real file. In a Rush sandbox the agent runs the Pest suite against SQLite with fake uploads, runs larastan and pint, and keeps every command and its output in the run's record.

The change uses Filament's own `Importer`, `ImportColumn` and `ImportAction` and the tables already migrated, so nothing is added to `composer.json` and the importer reads like the one beside it. The same request works on any Filament 5 resource.

## Frequently asked questions

### What identifies an existing product?

The `sku`, through `firstOrNew` in `resolveRecord()`, the way the category importer uses `slug`. A row whose `sku` matches updates that product; a new `sku` creates one. Change the key in that one method if your catalogue identifies products differently.

### How are categories imported if relationship() only resolves belongsTo?

The file carries a column of category slugs separated by commas. `afterSave()` looks the slugs up and syncs `productCategories()` without detaching, so a product keeps categories the file does not mention and gains the ones it does.

### Does the import run in the request or on a queue?

Whatever `QUEUE_CONNECTION` says. The demo's `.env.example` sets `sync`, so rows import before the response; with a real queue the rows run as jobs and the completed notification arrives through the panel's database notifications.

### What happens to rows that fail validation?

They are written to `failed_import_rows` with the validation message, the import's counts show them, and the notification offers the failed rows as a CSV to fix and upload again. The good rows in the same file import as normal.

## More Filament jobs

- [Add Email Verification to a Filament Panel from a Linear Issue](https://getrush.ai/run/add-email-verification/filament/)
- [Add Internationalisation to a Filament Admin Panel from a Linear Issue](https://getrush.ai/run/add-i18n/filament/)
- [Email Customers When an Order Status Changes in a Filament Shop](https://getrush.ai/run/add-order-status-emails/filament/)

## More jobs like this: Hand off a long task

- [Add Cursor Pagination to an Actix Web API](https://getrush.ai/run/add-cursor-pagination/actix/)
- [Add Cursor Pagination to an aiohttp App](https://getrush.ai/run/add-cursor-pagination/aiohttp/)
- [Add Cursor Pagination to an Axum API](https://getrush.ai/run/add-cursor-pagination/axum/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
