Updated import validation functionality
This commit is contained in:
@@ -3,10 +3,13 @@
|
||||
namespace App\Filament\Imports;
|
||||
|
||||
use App\Models\Block;
|
||||
use App\Models\Plant;
|
||||
use Filament\Actions\Imports\Exceptions\RowImportFailedException;
|
||||
use Filament\Actions\Imports\ImportColumn;
|
||||
use Filament\Actions\Imports\Importer;
|
||||
use Filament\Actions\Imports\Models\Import;
|
||||
use Notification;
|
||||
use Str;
|
||||
|
||||
class BlockImporter extends Importer
|
||||
{
|
||||
@@ -33,15 +36,23 @@ class BlockImporter extends Importer
|
||||
|
||||
public function resolveRecord(): ?Block
|
||||
{
|
||||
$plant = \App\Models\Plant::where('name', $this->data['plant'])->first();
|
||||
$warnMsg = [];
|
||||
$plant = Plant::where('name', $this->data['plant'])->first();
|
||||
if (!$plant) {
|
||||
return null;
|
||||
$warnMsg[] = "Plant not found";
|
||||
// $warnMsg[] = "Plant '" . $this->data['plant'] . "' not found";
|
||||
}
|
||||
if (Str::length($this->data['name']) < 0) {
|
||||
$warnMsg[] = "Block name not found";
|
||||
}
|
||||
|
||||
if (!empty($warnMsg)) {
|
||||
throw new RowImportFailedException(implode(', ', $warnMsg));
|
||||
}
|
||||
return Block::updateOrCreate([
|
||||
'name' => $this->data['name'],
|
||||
'plant_id' => $plant->id
|
||||
]
|
||||
);
|
||||
]);
|
||||
// return Block::firstOrNew([
|
||||
// // Update existing records, matching them by `$this->data['column_name']`
|
||||
// 'email' => $this->data['email'],
|
||||
|
||||
Reference in New Issue
Block a user