Issues Importing Excel Data with BelongsTo Relationship in Filament

I am trying to import data from an Excel file into my laravel Filament (V3) application , but I’m having trouble with importing relationships. Specifically, I have a Stock model that has a belongsTo relationship with a Supplier model. During the import, I want to set the supplier_id based on the supplier’s name provided in the Excel file.

Here is the code for my StockImporter class:

<?php

namespace AppFilamentImports;

use AppModelsStock;
use AppModelsSupplier;
use FilamentActionsImportsImportColumn;
use FilamentActionsImportsImporter;
use FilamentActionsImportsModelsImport;

class StockImporter extends Importer
{
    protected static ?string $model = Stock::class;

    public static function getColumns(): array
    {
        return [
            ImportColumn::make('name')
                ->requiredMapping()
                ->rules(['required', 'max:255']),
            ImportColumn::make('description'),
            ImportColumn::make('quantity')
                ->requiredMapping()
                ->numeric()
                ->rules(['required', 'integer']),
            ImportColumn::make('minimum_quantity')
                ->requiredMapping()
                ->numeric()
                ->rules(['required', 'integer']),
            ImportColumn::make('supplier_id')
                ->relationship(resolveUsing: function (string $state): ?Supplier {
                    $supplier = Supplier::query()
                        ->where('name', $state)
                        ->first();

                    return $supplier ? $supplier->id : null;
                }),
            ImportColumn::make('order_id')
                ->rules(['max:255']),
            ImportColumn::make('supplier_order_id')
                ->rules(['max:255']),
            ImportColumn::make('purchase_price')
                ->numeric()
                ->rules(['integer']),
            ImportColumn::make('sale_price')
                ->numeric()
                ->rules(['integer']),
        ];
    }

    public function resolveRecord(): ?Stock
    {
        // return Stock::firstOrNew([
        //     // Update existing records, matching them by `$this->data['column_name']`
        //     'email' => $this->data['email'],
        // ]);

        return new Stock();
    }

    public static function getCompletedNotificationBody(Import $import): string
    {
        $body = 'Your stock import has completed and ' . number_format($import->successful_rows) . ' ' . str('row')->plural($import->successful_rows) . ' imported.';

        if ($failedRowsCount = $import->getFailedRowsCount()) {
            $body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to import.';
        }

        return $body;
    }
}

quick summary:

  • I am using Filament for handling the import.
  • The Stock model has a belongsTo relationship with the Supplier model.
  • I want to import the supplier_id based on the supplier’s name provided in the Excel file.

Any help or suggestions would be greatly appreciated!