1
0
forked from poc/pds
Files
poc-pds1/app/Filament/Imports/LineImporter.php
2025-04-25 12:55:28 +05:30

69 lines
2.2 KiB
PHP

<?php
namespace App\Filament\Imports;
use App\Models\Line;
use Filament\Actions\Imports\ImportColumn;
use Filament\Actions\Imports\Importer;
use Filament\Actions\Imports\Models\Import;
class LineImporter extends Importer
{
protected static ?string $model = Line::class;
public static function getColumns(): array
{
return [
ImportColumn::make('name')
->requiredMapping()
->exampleHeader('Line Name')
->example('4 inch pump line')
->label('Line Name')
->rules(['required']),
ImportColumn::make('type')
->requiredMapping()
->exampleHeader('Line Type')
->example('Domestic Assembly')
->label('Line Type')
->rules(['required']),
ImportColumn::make('plant')
->requiredMapping()
->exampleHeader('Plant Name')
->example('Ransar Industries-I')
->label('Plant Name')
->relationship(resolveUsing:'name')
->rules(['required']),
];
}
public function resolveRecord(): ?Line
{
$plant = \App\Models\Plant::where('name', $this->data['plant'])->first();
return Line::updateOrCreate([
'name' => $this->data['name'],
'plant_id' => $plant->id
],
[
'type' => $this->data['type']
]
);
// return Line::firstOrNew([
// // Update existing records, matching them by `$this->data['column_name']`
// 'email' => $this->data['email'],
// ]);
// return new Line();
}
public static function getCompletedNotificationBody(Import $import): string
{
$body = 'Your line 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;
}
}